Pandas DataFrame rename() Method
In this tutorial, we will discuss and learn the Python pandas DataFrame.rename()
method. By using this method, we can alter or change the axes labels. When this method applied to the DataFrame, it returns a DataFrame or None if the inplace=True
. It raises a KeyError
if the specified label not found in the selected axis.
The below shows the syntax of the DataFrame.rename()
method.
Syntax
DataFrame.rename(mapper=None, index=None, columns=None, axis=None, copy=True, inplace=False, level=None, errors='ignore')
Parameters
mapper: It can be a dict-like or function. Dict-like or function transformations to apply to that axis’ values. Use either mapper
and axis
to specify the axis to target with mapper
, or index
and columns
.
index: It can be a dict-like or function. It is the alternative to specifying the axis.
columns: It can be a dict-like or function. It is the alternative to specifying the axis.
axis: It represents index or column axis, '0' or 'index' for index and '1' or 'columns' for the column axis. When the axis=0 or index
, method applied over the index
axis and when the axis=1
or columns
method applied over the column
axis.
copy: It represents the bool(True or False), and the default is True. It copies the underlying data.
inplace: It represents the bool(True or False), and the default is False. It indicates whether to return a new DataFrame or not. If the parameter is True then the value of the copy is ignored.
level: It indicates the int or level name, and the default is None.
errors: It includes ‘ignore’, ‘raise’, and the default is ‘ignore’.
Example 1: Alter the label of the DataFrame in Pandas
Here, using the DataFrame.rename()
method, we change the name of the column. See the below example.
We can also specify in which axis we are aletering using the axis parameter.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6],"C": [7,8,9]})
print("-------The DataFrame is-------")
print(df)
print("----------------------------------")
print(df.rename(columns={"A": "a", "B": "b","C":"c"}))
-------The DataFrame is-------
A B C
0 1 4 7
1 2 5 8
2 3 6 9
----------------------------------
a b c
0 1 4 7
1 2 5 8
2 3 6 9
Example: Alter the label using the DataFrame.rename()
Method
Here, using the DataFrame.rename()
method, we change the name of the index. See the below example.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6],"C": [7,8,9]})
print("-------The DataFrame is-------")
print(df)
print("----------------------------------")
print(df.rename(index={0: "index_1", 1: "index_2", 2:"index_3"}))
-------The DataFrame is-------
A B C
0 1 4 7
1 2 5 8
2 3 6 9
----------------------------------
A B C
index_1 1 4 7
index_2 2 5 8
index_3 3 6 9
Example: Set the errors=raise
in the DataFrame.rename()
Method
If any error occurs while altering the name of the row or column, by default the DataFrame.rename()
method ignores the errors. If we set the errors='raise'
in the DataFrame.rename()
method, it raises error. See the below example.
In the below example, the DataFrame.rename()
method raises the KeyError as the specified column label is not found in the DataFrame.
#importing pandas as pd
import pandas as pd
#creating the DataFrame
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6],"C": [7,8,9]})
print(df.rename(columns={"A": "a", "B": "b","D":"d"},errors="raise"))
KeyError: "['D'] not found in axis"
Conclusion
In this tutorial, we learned the Python pandas DataFrame.rename()
method. We learned syntax, parameters, and solved examples by applying this method on the DataFrame and understood the method.