Pandas Series: rename_axis() function
Set the name of the axis in Pandas
The rename_axis() function is used to set the name of the axis for the index or columns.
Syntax:
Series.rename_axis(self, mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False)
Parameters:
Name | Description | Type/Default Value | Required / Optional |
---|---|---|---|
mapper | Value to set the axis name attribute. | scalar, list-like | optional |
index, columns | 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. |
scalar, list-like, dict-like or function, | optional |
axis | The axis to rename. | {0 or ‘index’, 1 or ‘columns’} Default Value: 0 |
Required |
copy | Also copy underlying data. | bool Default Value: True |
Required |
inplace | Modifies the object directly, instead of creating a new Series or DataFrame. | bool Default Value: False |
Required |
Returns: Series,DataFrame, or None - The same type as the caller or None if inplace is True.
Notes: DataFrame.rename_axis supports two calling conventions
- (index=index_mapper, columns=columns_mapper, ...)
- mapper, axis={'index', 'columns'}, ...)
The second calling convention will modify the names of the the corresponding index if mapper is a list or a scalar. However, if mapper is dict-like or a function, it will use the deprecated behavior of modifying the axis labels.
We highly recommend using keyword arguments to clarify your intent.
Example - Series:
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(["lion", "fox", "monkey"])
s
Output:
0 lion 1 fox 2 monkey dtype: object
Python-Pandas Code:
import numpy as np
import pandas as pd
s = pd.Series(["lion", "fox", "monkey"])
s.rename_axis("animal")
Output:
animal 0 lion 1 fox 2 monkey dtype: object
Example - DataFrame:
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({"num_legs": [4, 4, 2],
"num_arms": [0, 0, 2]},
["lion", "fox", "monkey"])
df
Output:
num_legs num_arms lion 4 0 fox 4 0 monkey 2 2
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({"num_legs": [4, 4, 2],
"num_arms": [0, 0, 2]},
["lion", "fox", "monkey"])
df = df.rename_axis("animal")
df
Output:
num_legs num_arms animal lion 4 0 fox 4 0 monkey 2 2
Python-Pandas Code:
import numpy as np
import pandas as pd
df = pd.DataFrame({"num_legs": [4, 4, 2],
"num_arms": [0, 0, 2]},
["lion", "fox", "monkey"])
df = df.rename_axis("limbs", axis="columns")
df
Output:
limbs num_legs num_arms animal lion 4 0 fox 4 0 monkey 2 2
Example - MultiIndex:
Python-Pandas Code:
import numpy as np
import pandas as pd
df.index = pd.MultiIndex.from_product([['mammal'],
['lion', 'fox', 'monkey']],
names=['type', 'name'])
df
Output:
imbs num_legs num_arms type name mammal lion 4 0 fox 4 0 monkey 2 2
Python-Pandas Code:
import numpy as np
import pandas as pd
df.index = pd.MultiIndex.from_product([['mammal'],
['lion', 'fox', 'monkey']],
names=['type', 'name'])
df.rename_axis(index={'type': 'class'})
Output:
limbs num_legs num_arms class name mammal lion 4 0 fox 4 0 monkey 2 2
Python-Pandas Code:
import numpy as np
import pandas as pd
df.index = pd.MultiIndex.from_product([['mammal'],
['lion', 'fox', 'monkey']],
names=['type', 'name'])
df.rename_axis(columns=str.upper)
Output:
LIMBS num_legs num_arms type name mammal lion 4 0 fox 4 0 monkey 2 2
Previous: Alter Series index in Pandas
Next: Generate a new Pandas series with the index reset
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/pandas/series/series-rename_axis.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics