
- 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 - Renaming the columns of Pandas DataFrame
To rename the columns of a DataFrame, use the rename() method. Set the column names you want to rename under the “columns” parameter of the rename() method. For example, changing “Car” column to “Car Name” −
dataFrame.rename(columns={'Car': 'Car Name'}, inplace=False)
At first, read the CSV and create a DataFrame −
dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv")
Now, rename the column names. Here, we are renaming the columns “Car”, “Date_of_Purchase” and “Reg_Price” −
dataFrame = dataFrame.rename(columns={'Car': 'Car Name', 'Date_of_Purchase': 'Sold On', 'Reg_Price' : 'Booking Price'}, inplace=False)
Example
Following is the code
import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\Users\amit_\Desktop\SalesRecords.csv") print("DataFrame...\n",dataFrame) # count the rows and columns in a DataFrame print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape) dataFrame = dataFrame.rename(columns={'Car': 'Car Name', 'Date_of_Purchase': 'Sold On', 'Reg_Price' : 'Booking Price'}, inplace=False) print("\nDataFrame with updated Column Name ...\n",dataFrame)
Output
This will produce the following output −
DataFrame... Car Date_of_Purchase Reg_Price 0 BMW 10/10/2020 1000 1 Lexus 10/12/2020 750 2 Audi 10/17/2020 750 3 Jaguar 10/16/2020 1500 4 Mustang 10/19/2020 1100 5 Lamborghini 10/22/2020 1000 Number of rows and column in our DataFrame = (6, 3) DataFrame with updated Column Name ... Car Name Sold On Booking Price 0 BMW 10/10/2020 1000 1 Lexus 10/12/2020 750 2 Audi 10/17/2020 750 3 Jaguar 10/16/2020 1500 4 Mustang 10/19/2020 1100 5 Lamborghini 10/22/2020 1000
- Related Articles
- Python Pandas - Query the columns of a DataFrame
- Python - Grouping columns in Pandas Dataframe
- Renaming column names – Python Pandas
- Python Pandas – Get the datatype and DataFrame columns information
- Python - Select multiple columns from a Pandas dataframe
- Python - Name columns explicitly in a Pandas DataFrame
- Python Pandas – Count the rows and columns in a DataFrame
- Python Pandas - Filtering columns from a DataFrame on the basis of sum
- Python Pandas - Plot multiple data columns in a DataFrame?
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns
- How to change the order of Pandas DataFrame columns?
- Plot multiple columns of Pandas DataFrame using Seaborn
- Select multiple columns in a Pandas DataFrame
- How to sort multiple columns of a Pandas DataFrame?
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns but avoid setting the index of the returned DataFrame

Advertisements