
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 Questions & Answers
- Python Pandas - Query the columns of a DataFrame
- Python - Grouping columns in Pandas Dataframe
- Renaming column names – Python Pandas
- How to change the order of Pandas DataFrame columns?
- Python - Select multiple columns from a Pandas dataframe
- Python - Name columns explicitly in a Pandas DataFrame
- Python Pandas – Get the datatype and DataFrame columns information
- Python Pandas - Filtering columns from a DataFrame on the basis of sum
- Python Pandas - Create a DataFrame with the levels of the MultiIndex as columns
- Plot multiple columns of Pandas DataFrame using Seaborn
- Python Pandas - Plot multiple data columns in a DataFrame?
- Python Pandas – Count the rows and columns in a DataFrame
- How to sort multiple columns of a Pandas DataFrame?
- Select multiple columns in 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