How to rename multiple column headers in a Pandas DataFrame?
Here we are going to rename multiple column headers using the rename() method. The rename method is used to rename a single column as well as rename multiple columns at a time. And pass columns that contain the new values and in place = true as an argument. We pass inplace = true because we just modify the working data frame if we pass inplace = false then it returns a new dataframe.
Ways to Rename Pandas Columns in Python
There are various ways of renaming multiple column headers in a Pandas Dataframe, we are discussing some important methods which are the following
- Rename multiple column headers using rename() method
- Rename Multiple Columns by Names in Pandas
- Changing Multiple Column Names By Index
- Rename Multiple Columns using a Dictionary
- Rename Multiple Columns using
str.replace
()
Rename multiple column headers using rename() method
In this example, we Rename a Column in Pandas DataFrame, the below code creates a Pandas DataFrame with columns for names, locations, and pay. It then renames the columns and displays both the original and modified data.
Python
# import pandas import pandas as pd # create data frame df = pd.DataFrame({ 'First Name' : [ "Mukul" , "Rohan" , "Mayank" , "Vedansh" , "Krishna" ], 'Location' : [ "Saharanpur" , "Rampur" , "Agra" , "Saharanpur" , "Noida" ], 'Pay' : [ 56000.0 , 55000.0 , 61000.0 , 45000.0 , 62000.0 ]}) # print original data frame display(df) # create a dictionary # key = old name # value = new name dict = { 'First Name' : 'Name' , 'Location' : 'City' , 'Pay' : 'Salary' } # call rename () method df.rename(columns = dict , inplace = True ) # print Data frame after rename columns display(df) |
Output:
Original DataFrame:
First Name Location Pay
0 Mukul Saharanpur 56000.0
1 Rohan Rampur 55000.0
2 Mayank Agra 61000.0
3 Vedansh Saharanpur 45000.0
4 Krishna Noida 62000.0
DataFrame after renaming columns by index:
name address Pay
0 Mukul Saharanpur 56000.0
1 Rohan Rampur 55000.0
2 Mayank Agra 61000.0
3 Vedansh Saharanpur 45000.0
4 Krishna Noida 62000.0
Rename Multiple Columns by Names in Pandas
In this example, Assigning List of new column names. Rename and Modify Column in Pandas DataFrame , the below code involves the creation of a Pandas DataFrame, initial display, renaming of columns, and subsequent modification with a second set of column names, followed by the final display of the modified DataFrame.
Python3
# import pandas import pandas as pd # create data frame df = pd.DataFrame({ 'First Name' : [ "Mukul" , "Rohan" , "Mayank" , "Vedansh" , "Krishna" ], 'Location' : [ "Saharanpur" , "Rampur" , "Agra" , "Saharanpur" , "Noida" ], 'Pay' : [ 56000.0 , 55000.0 , 61000.0 , 45000.0 , 62000.0 ]}) print ( "Original DataFrame" ) # print original data frame display(df) # create a dictionary # key = old name # value = new name dict = { 'First Name' : 'Name' , 'Location' : 'City' , 'Pay' : 'Salary' } print ( "\nAfter rename" ) # call rename () method df.rename(columns = dict , inplace = True ) # print Data frame after rename columns display(df) # create a dictionary # key = old name # value = new name dict = { 'Name' : 'Full Name' , 'City' : 'Address' , 'Salary' : 'Amount' } # call rename () method df.rename(columns = dict , inplace = True ) display(df) |
Output:
Original DataFrame
First Name Location Pay
0 Mukul Saharanpur 56000.0
1 Rohan Rampur 55000.0
2 Mayank Agra 61000.0
3 Vedansh Saharanpur 45000.0
4 Krishna Noida 62000.0
After rename
Name City Salary
0 Mukul Saharanpur 56000.0
1 Rohan Rampur 55000.0
2 Mayank Agra 61000.0
3 Vedansh Saharanpur 45000.0
4 Krishna Noida 62000.0
Changing Multiple Column Names By Index
In this example the below code uses the pandas library in Python to create a DataFrame, print the original DataFrame, and then rename the first two columns (“First Name” and “Location”) by index. Below is the expected output of the code:
Python3
# import pandas import pandas as pd # create data frame df = pd.DataFrame({ 'First Name' : [ "Mukul" , "Rohan" , "Mayank" , "Vedansh" , "Krishna" ], 'Location' : [ "Saharanpur" , "Rampur" , "Agra" , "Saharanpur" , "Noida" ], 'Pay' : [ 56000.0 , 55000.0 , 61000.0 , 45000.0 , 62000.0 ]}) # print original data frame display(df) # renaming the column by index df.columns.values[ 0 : 2 ] = [ "name" , "address" ] # print Data frame after rename columns display(df) |
Output:
First Name Location Pay
0 Mukul Saharanpur 56000.0
1 Rohan Rampur 55000.0
2 Mayank Agra 61000.0
3 Vedansh Saharanpur 45000.0
4 Krishna Noida 62000.0
name address Pay
0 Mukul Saharanpur 56000.0
1 Rohan Rampur 55000.0
2 Mayank Agra 61000.0
3 Vedansh Saharanpur 45000.0
4 Krishna Noida 62000.0
Rename Multiple Columns using a Dictionary
In this example we use the columns
attribute of the DataFrame along with a dictionary to rename multiple column headers. This approach allows you to map the old column names to the new ones. below code creates a Pandas DataFrame and renames columns ‘A’ to ‘X’, ‘B’ to ‘Y’, ‘C’ to ‘Z’, then prints the result.
Python3
import pandas as pd # Sample DataFrame data = { 'A' : [ 1 , 2 , 3 ], 'B' : [ 4 , 5 , 6 ], 'C' : [ 7 , 8 , 9 ]} df = pd.DataFrame(data) # Create a dictionary to map old column names to new ones column_mapping = { 'A' : 'X' , 'B' : 'Y' , 'C' : 'Z' } # Rename columns using the dictionary and columns attribute df.columns = [column_mapping[col] if col in column_mapping else col for col in df.columns] # Display the modified DataFrame print (df) |
Output :
X Y Z
0 1 4 7
1 2 5 8
2 3 6 9
Rename Multiple Columns using str.replace
()
In this example we use the str.replace
method on the column names to replace specific substrings with new ones. This method is useful when you want to perform simple string replacements. Below code utilizes Pandas to create a DataFrame, renaming columns by replacing ‘Product_A_’ with ‘Item_’ using `str.replace`, and prints the modified DataFrame.
Python3
import pandas as pd # Sample DataFrame data = { 'Product_A_Name' : [ 1 , 2 , 3 ], 'Product_B_Price' : [ 4 , 5 , 6 ], 'Product_C_Quantity' : [ 7 , 8 , 9 ]} df = pd.DataFrame(data) # Use str.replace to rename columns by replacing substrings df.columns = df.columns. str .replace( 'Product_A_' , 'Item_' ). str .replace( 'Product_B_' , 'Price_' ). str .replace( 'Product_C_' , 'Qty_' ) # Display the modified DataFrame print (df) |
Output :
Item_Name Price_Price Qty_Quantity
0 1 4 7
1 2 5 8
2 3 6 9
Contact Us