DataFrame.
rename_axis
Set the name of the axis for the index or columns.
A scalar, list-like, dict-like or functions transformations to apply to the axis name attribute.
A scalar, list-like, dict-like or functions transformations to apply to that axis’ values.
Use either mapper and axis to specify the axis to target with mapper, or index and/or columns.
mapper
axis
index
columns
The axis to rename.
Modifies the object directly, instead of creating a new DataFrame.
See also
Series.rename
Alter Series index labels or name.
DataFrame.rename
Alter DataFrame index labels or name.
Index.rename
Set new names on index.
Notes
DataFrame.rename_axis supports two calling conventions
DataFrame.rename_axis
(index=index_mapper, columns=columns_mapper, ...)
(mapper, axis={'index', 'columns'}, ...)
The first calling convention will only modify the names of the index and/or the names of the Index object that is the columns.
The second calling convention will modify the names of the corresponding index specified by axis.
We highly recommend using keyword arguments to clarify your intent.
Examples
>>> df = ps.DataFrame({"num_legs": [4, 4, 2], ... "num_arms": [0, 0, 2]}, ... index=["dog", "cat", "monkey"], ... columns=["num_legs", "num_arms"]) >>> df num_legs num_arms dog 4 0 cat 4 0 monkey 2 2
>>> df = df.rename_axis("animal").sort_index() >>> df num_legs num_arms animal cat 4 0 dog 4 0 monkey 2 2
>>> df = df.rename_axis("limbs", axis="columns").sort_index() >>> df limbs num_legs num_arms animal cat 4 0 dog 4 0 monkey 2 2
MultiIndex
>>> index = pd.MultiIndex.from_product([['mammal'], ... ['dog', 'cat', 'monkey']], ... names=['type', 'name']) >>> df = ps.DataFrame({"num_legs": [4, 4, 2], ... "num_arms": [0, 0, 2]}, ... index=index, ... columns=["num_legs", "num_arms"]) >>> df num_legs num_arms type name mammal dog 4 0 cat 4 0 monkey 2 2
>>> df.rename_axis(index={'type': 'class'}).sort_index() num_legs num_arms class name mammal cat 4 0 dog 4 0 monkey 2 2
>>> df.rename_axis(index=str.upper).sort_index() num_legs num_arms TYPE NAME mammal cat 4 0 dog 4 0 monkey 2 2