@@ -1109,35 +1109,41 @@ def rename(self, *args, **kwargs):
1109
1109
('inplace' , False )])
1110
1110
def rename_axis (self , mapper = None , ** kwargs ):
1111
1111
"""
1112
- Alter the name of the index or name of Index object that is the
1113
- columns.
1112
+ Set the name of the axis for the index or columns.
1114
1113
1115
1114
Parameters
1116
1115
----------
1117
1116
mapper : scalar, list-like, optional
1118
1117
Value to set the axis name attribute.
1119
1118
index, columns : scalar, list-like, dict-like or function, optional
1120
- dict-like or functions transformations to apply to
1121
- that axis' values.
1119
+ A scalar, list-like, dict-like or functions transformations to
1120
+ apply to that axis' values.
1122
1121
1123
1122
Use either ``mapper`` and ``axis`` to
1124
1123
specify the axis to target with ``mapper``, or ``index``
1125
1124
and/or ``columns``.
1126
1125
1127
1126
.. versionchanged:: 0.24.0
1128
1127
1129
- axis : int or string, default 0
1130
- copy : boolean, default True
1128
+ axis : {0 or 'index', 1 or 'columns'}, default 0
1129
+ The axis to rename.
1130
+ copy : bool, default True
1131
1131
Also copy underlying data.
1132
- inplace : boolean , default False
1132
+ inplace : bool , default False
1133
1133
Modifies the object directly, instead of creating a new Series
1134
1134
or DataFrame.
1135
1135
1136
1136
Returns
1137
1137
-------
1138
- renamed : Series, DataFrame, or None
1138
+ Series, DataFrame, or None
1139
1139
The same type as the caller or None if `inplace` is True.
1140
1140
1141
+ See Also
1142
+ --------
1143
+ Series.rename : Alter Series index labels or name.
1144
+ DataFrame.rename : Alter DataFrame index labels or name.
1145
+ Index.rename : Set new names on index.
1146
+
1141
1147
Notes
1142
1148
-----
1143
1149
Prior to version 0.21.0, ``rename_axis`` could also be used to change
@@ -1162,75 +1168,73 @@ def rename_axis(self, mapper=None, **kwargs):
1162
1168
We *highly* recommend using keyword arguments to clarify your
1163
1169
intent.
1164
1170
1165
- See Also
1166
- --------
1167
- Series.rename : Alter Series index labels or name.
1168
- DataFrame.rename : Alter DataFrame index labels or name.
1169
- Index.rename : Set new names on index.
1170
-
1171
1171
Examples
1172
1172
--------
1173
1173
**Series**
1174
1174
1175
- >>> s = pd.Series([1, 2, 3])
1176
- >>> s.rename_axis("foo")
1177
- foo
1178
- 0 1
1179
- 1 2
1180
- 2 3
1181
- dtype: int64
1175
+ >>> s = pd.Series(["dog", "cat", "monkey"])
1176
+ >>> s
1177
+ 0 dog
1178
+ 1 cat
1179
+ 2 monkey
1180
+ dtype: object
1181
+ >>> s.rename_axis("animal")
1182
+ animal
1183
+ 0 dog
1184
+ 1 cat
1185
+ 2 monkey
1186
+ dtype: object
1182
1187
1183
1188
**DataFrame**
1184
1189
1185
- >>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
1186
- >>> df.rename_axis("foo")
1187
- A B
1188
- foo
1189
- 0 1 4
1190
- 1 2 5
1191
- 2 3 6
1192
-
1193
- >>> df.rename_axis("bar", axis="columns")
1194
- bar A B
1195
- 0 1 4
1196
- 1 2 5
1197
- 2 3 6
1198
-
1199
- >>> mi = pd.MultiIndex.from_product([['a', 'b', 'c'], [1, 2]],
1200
- ... names=['let','num'])
1201
- >>> df = pd.DataFrame({'x': [i for i in range(len(mi))],
1202
- ... 'y' : [i*10 for i in range(len(mi))]},
1203
- ... index=mi)
1204
- >>> df.rename_axis(index={'num' : 'n'})
1205
- x y
1206
- let n
1207
- a 1 0 0
1208
- 2 1 10
1209
- b 1 2 20
1210
- 2 3 30
1211
- c 1 4 40
1212
- 2 5 50
1213
-
1214
- >>> cdf = df.rename_axis(columns='col')
1215
- >>> cdf
1216
- col x y
1217
- let num
1218
- a 1 0 0
1219
- 2 1 10
1220
- b 1 2 20
1221
- 2 3 30
1222
- c 1 4 40
1223
- 2 5 50
1224
-
1225
- >>> cdf.rename_axis(columns=str.upper)
1226
- COL x y
1227
- let num
1228
- a 1 0 0
1229
- 2 1 10
1230
- b 1 2 20
1231
- 2 3 30
1232
- c 1 4 40
1233
- 2 5 50
1190
+ >>> df = pd.DataFrame({"num_legs": [4, 4, 2],
1191
+ ... "num_arms": [0, 0, 2]},
1192
+ ... ["dog", "cat", "monkey"])
1193
+ >>> df
1194
+ num_legs num_arms
1195
+ dog 4 0
1196
+ cat 4 0
1197
+ monkey 2 2
1198
+ >>> df = df.rename_axis("animal")
1199
+ >>> df
1200
+ num_legs num_arms
1201
+ animal
1202
+ dog 4 0
1203
+ cat 4 0
1204
+ monkey 2 2
1205
+ >>> df = df.rename_axis("limbs", axis="columns")
1206
+ >>> df
1207
+ limbs num_legs num_arms
1208
+ animal
1209
+ dog 4 0
1210
+ cat 4 0
1211
+ monkey 2 2
1212
+
1213
+ **MultiIndex**
1214
+
1215
+ >>> df.index = pd.MultiIndex.from_product([['mammal'],
1216
+ ... ['dog', 'cat', 'monkey']],
1217
+ ... names=['type', 'name'])
1218
+ >>> df
1219
+ limbs num_legs num_arms
1220
+ type name
1221
+ mammal dog 4 0
1222
+ cat 4 0
1223
+ monkey 2 2
1224
+
1225
+ >>> df.rename_axis(index={'type': 'class'})
1226
+ limbs num_legs num_arms
1227
+ class name
1228
+ mammal dog 4 0
1229
+ cat 4 0
1230
+ monkey 2 2
1231
+
1232
+ >>> df.rename_axis(columns=str.upper)
1233
+ LIMBS num_legs num_arms
1234
+ type name
1235
+ mammal dog 4 0
1236
+ cat 4 0
1237
+ monkey 2 2
1234
1238
"""
1235
1239
axes , kwargs = self ._construct_axes_from_arguments ((), kwargs )
1236
1240
copy = kwargs .pop ('copy' , True )
@@ -1285,45 +1289,57 @@ def rename_axis(self, mapper=None, **kwargs):
1285
1289
1286
1290
def _set_axis_name (self , name , axis = 0 , inplace = False ):
1287
1291
"""
1288
- Alter the name or names of the axis.
1292
+ Set the name(s) of the axis.
1289
1293
1290
1294
Parameters
1291
1295
----------
1292
1296
name : str or list of str
1293
- Name for the Index, or list of names for the MultiIndex
1294
- axis : int or str
1295
- 0 or 'index' for the index; 1 or 'columns' for the columns
1296
- inplace : bool
1297
- whether to modify `self` directly or return a copy
1297
+ Name(s) to set.
1298
+ axis : {0 or 'index', 1 or 'columns'}, default 0
1299
+ The axis to set the label. The value 0 or 'index' specifies index,
1300
+ and the value 1 or 'columns' specifies columns.
1301
+ inplace : bool, default False
1302
+ If `True`, do operation inplace and return None.
1298
1303
1299
1304
.. versionadded:: 0.21.0
1300
1305
1301
1306
Returns
1302
1307
-------
1303
- renamed : same type as caller or None if inplace=True
1308
+ Series, DataFrame, or None
1309
+ The same type as the caller or `None` if `inplace` is `True`.
1304
1310
1305
1311
See Also
1306
1312
--------
1307
- pandas.DataFrame.rename
1308
- pandas.Series.rename
1309
- pandas.Index.rename
1313
+ DataFrame.rename : Alter the axis labels of :class:`DataFrame`.
1314
+ Series.rename : Alter the index labels or set the index name
1315
+ of :class:`Series`.
1316
+ Index.rename : Set the name of :class:`Index` or :class:`MultiIndex`.
1310
1317
1311
1318
Examples
1312
1319
--------
1313
- >>> df._set_axis_name("foo")
1314
- A
1315
- foo
1316
- 0 1
1317
- 1 2
1318
- 2 3
1319
- >>> df.index = pd.MultiIndex.from_product([['A'], ['a', 'b', 'c']])
1320
- >>> df._set_axis_name(["bar", "baz"])
1321
- A
1322
- bar baz
1323
- A a 1
1324
- b 2
1325
- c 3
1326
- """
1320
+ >>> df = pd.DataFrame({"num_legs": [4, 4, 2]},
1321
+ ... ["dog", "cat", "monkey"])
1322
+ >>> df
1323
+ num_legs
1324
+ dog 4
1325
+ cat 4
1326
+ monkey 2
1327
+ >>> df._set_axis_name("animal")
1328
+ num_legs
1329
+ animal
1330
+ dog 4
1331
+ cat 4
1332
+ monkey 2
1333
+ >>> df.index = pd.MultiIndex.from_product(
1334
+ ... [["mammal"], ['dog', 'cat', 'monkey']])
1335
+ >>> df._set_axis_name(["type", "name"])
1336
+ legs
1337
+ type name
1338
+ mammal dog 4
1339
+ cat 4
1340
+ monkey 2
1341
+ """
1342
+ pd .MultiIndex .from_product ([["mammal" ], ['dog' , 'cat' , 'monkey' ]])
1327
1343
axis = self ._get_axis_number (axis )
1328
1344
idx = self ._get_axis (axis ).set_names (name )
1329
1345
0 commit comments