
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - How to rename multiple column headers in a Pandas DataFrame with Dictionary?
To rename multiple column headers, use the rename() method and set the dictionary in the columns parameter. At first, let us create a DataFrame −
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] })
Creating a dictionary to rename columns. The key and value pairs as old name and new name −
dictionary = {'Car': 'Car Name','Cubic Capacity': 'CC','Reg Price': 'Registration Price','Units Sold': 'Units Purchased' }
Use rename() and set the dictionary as columns −
dataFrame.rename(columns=dictionary, inplace=True)
Example
Following is the code −
import pandas as pd # creating dataframe 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"DataFrame ...\n",dataFrame # creating a dictionary to rename columns # key and value pairs as old name and new name dictionary = {'Car': 'Car Name','Cubic Capacity': 'CC','Reg Price': 'Registration Price','Units Sold': 'Units Purchased' } # using rename() and setting the dictionary as columns dataFrame.rename(columns=dictionary, inplace=True) print"\nUpdated DataFrame ...\n",dataFrame
Output
This will produce the following output −
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 Updated DataFrame ... 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
- Related Articles
- How to rename column names in a Pandas DataFrame?
- Python – Center align column headers of a Pandas DataFrame
- Python - Rename column names by index in a Pandas DataFrame without using rename()
- How to get the list of column headers from a Pandas DataFrame?
- Python – Drop multiple levels from a multi-level column index in Pandas dataframe
- Python Pandas - How to select multiple rows from a DataFrame
- Python - How to select a column from a Pandas DataFrame
- Python Pandas - Convert Nested Dictionary to Multiindex Dataframe
- How to convert a DataFrame into a dictionary in Pandas?
- How to create a pandas DataFrame using a dictionary?
- Python - Add a new column with constant value to Pandas DataFrame
- Python - Add a zero column to Pandas DataFrame
- Apply uppercase to a column in Pandas dataframe in Python
- Python Pandas - Plot multiple data columns in a DataFrame?
- Python - Select multiple columns from a Pandas dataframe

Advertisements