Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary?
In Pandas, you can rename multiple column headers simultaneously using the rename() method with a dictionary. The dictionary maps old column names (keys) to new column names (values).
Syntax
DataFrame.rename(columns=dictionary, inplace=True)
Where dictionary contains old_name: new_name pairs, and inplace=True modifies the original DataFrame.
Creating a Sample DataFrame
Let's start by creating a DataFrame with car data ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Mustang', 'Tesla', 'Mustang', 'Mercedes', 'Tesla', 'Audi'],
"Cubic Capacity": [2000, 1800, 1500, 2500, 2200, 3000, 2000],
"Reg Price": [7000, 1500, 5000, 8000, 9000, 6000, 1500],
"Units Sold": [200, 120, 150, 120, 210, 250, 220]
})
print("Original DataFrame:")
print(dataFrame)
Original DataFrame:
Car Cubic Capacity Reg Price Units Sold
0 BMW 2000 7000 200
1 Mustang 1800 1500 120
2 Tesla 1500 5000 150
3 Mustang 2500 8000 120
4 Mercedes 2200 9000 210
5 Tesla 3000 6000 250
6 Audi 2000 1500 220
Renaming Multiple Columns
Create a dictionary with old column names as keys and new names as values, then use rename() ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Mustang', 'Tesla', 'Mustang', 'Mercedes', 'Tesla', 'Audi'],
"Cubic Capacity": [2000, 1800, 1500, 2500, 2200, 3000, 2000],
"Reg Price": [7000, 1500, 5000, 8000, 9000, 6000, 1500],
"Units Sold": [200, 120, 150, 120, 210, 250, 220]
})
# Dictionary mapping old names to new names
rename_dict = {
'Car': 'Car Name',
'Cubic Capacity': 'CC',
'Reg Price': 'Registration Price',
'Units Sold': 'Units Purchased'
}
# Rename columns using the dictionary
dataFrame.rename(columns=rename_dict, inplace=True)
print("DataFrame with renamed columns:")
print(dataFrame)
DataFrame with renamed columns: Car Name CC Registration Price Units Purchased 0 BMW 2000 7000 200 1 Mustang 1800 1500 120 2 Tesla 1500 5000 150 3 Mustang 2500 8000 120 4 Mercedes 2200 9000 210 5 Tesla 3000 6000 250 6 Audi 2000 1500 220
Alternative Approach Without inplace
You can also create a new DataFrame without modifying the original ?
import pandas as pd
dataFrame = pd.DataFrame({
"Car": ['BMW', 'Mustang', 'Tesla'],
"Price": [50000, 30000, 80000]
})
rename_dict = {'Car': 'Vehicle', 'Price': 'Cost'}
# Create new DataFrame with renamed columns
new_dataFrame = dataFrame.rename(columns=rename_dict)
print("Original DataFrame:")
print(dataFrame)
print("\nNew DataFrame:")
print(new_dataFrame)
Original DataFrame:
Car Price
0 BMW 50000
1 Mustang 30000
2 Tesla 80000
New DataFrame:
Vehicle Cost
0 BMW 50000
1 Mustang 30000
2 Tesla 80000
Conclusion
Use rename(columns=dictionary) to rename multiple columns at once. Set inplace=True to modify the original DataFrame, or omit it to create a new DataFrame with renamed columns.
Advertisements
