Sometimes we want to rename columns and indexes in the Pandas DataFrame object. We can use pandas DataFrame rename() function to rename columns and indexes. It supports the following parameters.
Some important points about rename() function.
Let’s look into some examples of using Pandas rename() function.
import pandas as pd
d1 = {'Name': ['Pankaj', 'Lisa', 'David'], 'ID': [1, 2, 3], 'Role': ['CEO', 'Editor', 'Author']}
df = pd.DataFrame(d1)
print('Source DataFrame:\n', df)
# rename columns
df1 = df.rename(columns={'Name': 'EmpName', 'ID': 'EmpID', 'Role': 'EmpRole'})
print('Result DataFrame:\n', df1)
Output:
Source DataFrame:
Name ID Role
0 Pankaj 1 CEO
1 Lisa 2 Editor
2 David 3 Author
Result DataFrame:
EmpName EmpID EmpRole
0 Pankaj 1 CEO
1 Lisa 2 Editor
2 David 3 Author
The above rename() function call can also be written in the following way.
df1 = df.rename(mapper={'Name': 'EmpName', 'ID': 'EmpID', 'Role': 'EmpRole'},
axis='columns') # axis=1 corresponds to columns
It’s clear that using the keyword arguments is clearer than using the mapper and axis arguments.
If you want to rename a single column, just pass the single key-value pair in the columns dict parameter.
df1 = df.rename(columns={'Name': 'EmpName'})
print(df1)
Output:
EmpName ID Role
0 Pankaj 1 CEO
1 Lisa 2 Editor
2 David 3 Author
The result will be the same if there is a non-matching mapping in the columns dictionary.
df1 = df.rename(columns={'Name': 'EmpName', 'X': 'Y'}) # same result since there is no X column
If you want to rename indexes, pass the dict for ‘index’ parameter.
df2 = df.rename(index={0: '#0', 1: '#1', 2: '#2'})
print('Renamed Indexes:\n', df2)
Output:
Renamed Indexes:
Name ID Role
#0 Pankaj 1 CEO
#1 Lisa 2 Editor
#2 David 3 Author
We can also rename indexes using mapper and axis arguments.
df2 = df.rename({0: '#0', 1: '#1', 2: '#2'}, axis=0)
# axis='index' will work, first argument is assigned to 'mapper'
df2 = df.rename(index={1: '#1'})
print(df2)
Output:
Name ID Role
0 Pankaj 1 CEO
#1 Lisa 2 Editor
2 David 3 Author
If you want to change the source DataFrame itself, pass the inplace argument as True.
import pandas as pd
d1 = {'Name': ['Pankaj', 'Lisa', 'David'], 'ID': [1, 2, 3], 'Role': ['CEO', 'Editor', 'Author']}
df = pd.DataFrame(d1)
print('Source DataFrame:\n', df)
df.rename(index={0: '#0', 1: '#1', 2: '#2'}, columns={'Name': 'EmpName', 'ID': 'EmpID', 'Role': 'EmpRole'}, inplace=True)
print('Source DataFrame:\n', df)
Output:
Source DataFrame:
Name ID Role
0 Pankaj 1 CEO
1 Lisa 2 Editor
2 David 3 Author
Source DataFrame:
EmpName EmpID EmpRole
#0 Pankaj 1 CEO
#1 Lisa 2 Editor
#2 David 3 Author
df = pd.DataFrame({'NAME': ['Pankaj', 'Lisa'], 'ID': [1, 2], 'ROLE': ['CEO', 'Editor']})
print(df)
df.rename(mapper=str.lower, axis=1, inplace=True)
print(df)
Output:
NAME ID ROLE
0 Pankaj 1 CEO
1 Lisa 2 Editor
name id role
0 Pankaj 1 CEO
1 Lisa 2 Editor
import pandas as pd
import math
df = pd.DataFrame({'NAME': ['Pankaj', 'Lisa'], 'ID': [1, 2], 'ROLE': ['CEO', 'Editor']})
df.rename(columns=str.lower, index=math.degrees, inplace=True)
print(df)
Output:
name id role
0.00000 Pankaj 1 CEO
57.29578 Lisa 2 Editor
import pandas as pd
df = pd.DataFrame({'NAME': ['Pankaj', 'Lisa'], 'ID': [1, 2], 'ROLE': ['CEO', 'Editor']})
df1 = df.rename(columns={'Salary': 'EmpSalary'}) # unmatched mappings are ignored
df1 = df.rename(columns={'Salary': 'EmpSalary'}, errors='raise') # unmatched mappings raising KeyError
Output:
Traceback (most recent call last):
File "/Users/pankaj/Documents/PycharmProjects/hello-world/journaldev/pandas/pandas_rename_column.py", line 58, in <module>
df1 = df.rename(columns={'Salary': 'EmpSalary'}, errors='raise')
KeyError: "['Salary'] not found in axis"
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.