• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Rename Columns in a Dataframe in Python

Rename Columns in a Dataframe in Python

Author: Aditya Raj
Last Updated: September 23, 2022

Pandas dataframes are one of the most efficient data structures to handle tabular data in python. When we import tabular data into dataframes from csv files, we usually need to rename the columns in the dataframes. In this article, we will discuss how we can rename columns in a dataframe in python.

Rename DataFrame Columns Using the rename() Method

The pandas module provides us with the rename() method to rename columns in a dataframe. The rename() method, when invoked on a dataframe, takes a python dictionary as its first input argument. The keys in the dictionary should consist of the original name of the columns that are to be renamed. The values associated with the keys should be the new column names.  After execution, the rename() method returns a new dataframe with the modified name. For example, we can rename the ‘Roll’ column of the given dataframe using the rename() method as shown in the following example.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The original column names are:")
print(df.columns.values)
nameDict={"Roll":"Roll No."}
df=df.rename(columns=nameDict)
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The original column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Name' 'Roll No.' 'Language']

If you want to rename multiple columns in the dataframe, you can pass the old column names and new column names in the dictionary as follows.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The original column names are:")
print(df.columns.values)
nameDict={"Name":"Person","Roll":"Roll No."}
df=df.rename(columns=nameDict)
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The original column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

In the above examples, the column names in the original columns aren’t modified. Instead, we get a new dataframe with the modified column names.

You can also rename the columns of the original dataframe. For this, we will use the ‘inplace’ parameter of the rename() method. The ‘inplace’ parameter takes an optional input argument and it has the default value False. Due to this, the column names in the original dataframe aren’t modified. You can set the ‘inplace’ parameter to the value True to modify the column names of the original dataframe as shown below.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The original column names are:")
print(df.columns.values)
nameDict={"Name":"Person","Roll":"Roll No."}
df.rename(columns=nameDict,inplace=True)
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The original column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

Suggested Reading: If you are into machine learning, you can read this article on regression in machine learning. You might also like this article on k-means clustering with numerical example.

Rename DataFrame Columns Using a List of Column Names

If you have to rename all the columns of the dataframes at once, you can do it using a python list. For this, we just have to assign the list containing the new dataframe names to the ‘columns’ attribute of the dataframe as shown below.

import pandas as pd
import numpy as np
df=pd.read_csv("demo_file.csv")
print("The dataframe is:")
print(df)
print("The original column names are:")
print(df.columns.values)
df.columns=['Person', 'Roll No.', 'Language']
print("The modified column names are:")
print(df.columns.values)

Output:

The dataframe is:
     Name  Roll    Language
0  Aditya     1      Python
1     Sam     2        Java
2   Chris     3         C++
3    Joel     4  TypeScript
The original column names are:
['Name' 'Roll' 'Language']
The modified column names are:
['Person' 'Roll No.' 'Language']

Conclusion

In this article, we have discussed how to rename columns in a dataframe in python. To know more about python programming, you can read this article on dictionary comprehension in python. You might also like this article on list comprehension in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us