Reference Materials
Certification Courses
Created with over a decade of experience and thousands of feedback.
Pandas rename()
In this tutorial, we will learn about the rename() method in Pandas with the help of examples.
The rename() method in Pandas is used to rename columns or index labels in a DataFrame.
Example
import pandas as pd
# create a DataFrame
data = {'Old_Column1': [1, 2, 3]}
df = pd.DataFrame(data)
# rename 'Old_Column1' to 'New_Column1'
result = df.rename(columns={'Old_Column1': 'New_Column1'})
print(result)
'''
Output
New_Column1
0 1
1 2
2 3
'''
rename() Syntax
The syntax of the rename() method in Pandas is:
df.rename(columns=None, index=None, inplace=False)
rename() Arguments
The rename() method takes following arguments:
columns(optional) - a dictionary that specifies the new names for columnsindex(optional) - a dictionary that specifies the new names for index labelsinplace(optional) - ifTrue, modifies the original DataFrame in place; ifFalse, returns a new DataFrame.
rename() Return Value
The rename() method returns a new DataFrame with the renamed columns and/or index labels.
Example 1: Rename Columns Using a Dictionary
import pandas as pd
data = {'Age': [25, 30, 35],
'Income': [50000, 60000, 75000]}
df = pd.DataFrame(data)
# rename columns 'Age' to 'Customer Age' and 'Income' to 'Annual Income'
df.rename(columns={'Age': 'Customer Age', 'Income': 'Annual Income'}, inplace=True)
print(df)
Output
Customer Age Annual Income
0 25 50000
1 30 60000
2 35 75000
In the above example, we have used the rename() method to rename the columns in the df DataFrame.
The columns parameter is set to a dictionary where the keys are the current column names Age and Income and the values are the new column names Customer Age and Annual Income.
The inplace=True argument modifies the DataFrame in place, so the original DataFrame df is updated with the new column names.
Example 2: Rename Index Labels Using a Dictionary
import pandas as pd
# create a sample DataFrame
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
# rename index labels using the dictionary
df.rename(index={0: 'Row1', 1: 'Row2', 2: 'Row3'}, inplace=True)
# display the DataFrame with renamed index labels
print(df)
Output
A B
Row1 1 4
Row2 2 5
Row3 3 6
In this example, we have used rename() on the df DataFrame to rename its index labels.
We have provided a dictionary to the index parameter, where the keys represent the current index labels 0, 1, 2, and the values represent the new index labels Row1, Row2, Row3.
Example 3: Rename Columns Using a Function
import pandas as pd
data = {'A': [1, 2, 3], 'B': [4, 5, 6]}
df = pd.DataFrame(data)
# define a function to rename columns
def column_rename_function(column_name):
# add a prefix 'new_' to the column name
return 'new_' + column_name
# rename columns using the function
df.rename(columns=column_rename_function, inplace=True)
print(df)
Output
new_A new_B
0 1 4
1 2 5
2 3 6
Here, we defined the function named column_rename_function() that adds a prefix "new_" to the column names.
Then, we pass this function to the rename() method's columns parameter, and when we set inplace=True, it modifies the df DataFrame to have columns with the new names.
The new DataFrame has new column names new_A and new_B.