Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Pandas Rename Column
Next article icon

Pandas Rename Column

Last Updated : 15 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Renaming columns in a Pandas DataFrame is a common operation when we want to clean, standardize, or transform data. In this article, we'll explore few different methods for renaming columns, each with specific use cases. Whether we're renaming a few columns or applying custom transformations, these methods offer flexible solutions for our needs.

The dataset we will use looks like this- Dataset.csv

Screenshot-2024-12-31-113842
Dataset Columns

Method 1: Renaming Column using Dictionary

rename() function is one of the most flexible methods for renaming columns. By passing a dictionary, where the keys are the current column names and the values are the new names, we can easily rename specific columns.

  • Use rename() with a dictionary to rename the columns.
Python
df = pd.read_csv('data.csv') df = df.rename(columns={'Age': 'Years', 'Gender': 'Sex'}) print(df) 

Output:

Screenshot-2024-12-31-122149
Renamed columns of the Dataset

This method is significant because it provides clarity and directness in renaming multiple columns simultaneously, making it efficient for larger datasets.

The other methods for renaming column names in dataset are:

Table of Content

  • Method 1: Renaming Column using Dictionary
  • Method 2: Renaming Columns by Assigning to columns Attribute
  • Method 3: Renaming Columns Using Axis Parameter
  • Method 4: Renaming Columns adding prefix or suffix
  • Method 5: Renaming Columns Using List Comprehension
  • Method 6: Renaming Columns by Replacing Specific Characters
  • Method 7: Renaming Columns by Mapping Functions

Method 2: Renaming Columns by Assigning to columns Attribute

With this method, we can directly assign a new list of column names to columns attribute. This approach is suitable when we want to replace all the column names at once.

  • Assign a new list of column names to df.columns.
Python
df = pd.read_csv('data.csv') df.columns = ['Full Name', 'Age in Years', 'Gender Identity', 'City of Residence'] print(df) 

Output:

Screenshot-2024-12-31-122620
Output

By assigning a new list to df.columns, we replace all the column names in one operation. This method is simple and quick, but be cautious: the length of the list must match the number of columns in the DataFrame.

Method 3: Renaming Columns Using Axis Parameter

The set_axis() method allows us to rename the columns by passing a new list of column names along with the axis=1 parameter. This method can be useful when we need to create a new DataFrame with the renamed columns.

  • Use set_axis() to rename the columns.
Python
df = pd.read_csv('data.csv') df = df.set_axis(['Name', 'Age', 'Gender', 'Location'], axis=1) print(df) 

This method is useful when we want to create a new DataFrame with renamed columns, especially when we don't want to modify the original DataFrame. Setting axis=1 targets column names.

Output:

Screenshot-2024-12-31-123239
Output

Method 4: Renaming Columns adding prefix or suffix

If we want to add a prefix or suffix to all column names, add_prefix() and add_suffix() methods are very handy. These methods are ideal when we want to modify all column names uniformly.

  • Use add_prefix() or add_suffix() to modify column names.
Python
df = pd.read_csv('data.csv') df = df.add_prefix('col_') print(df) 

Output:

Screenshot-2024-12-31-123505
Output

This is useful when we need to distinguish columns in a merged DataFrame or add identifiers to the column names.

Method 5: Renaming Columns Using List Comprehension

List comprehension is a flexible way to modify column names based on specific conditions. This is useful when we want to apply transformations such as converting all column names to uppercase, applying string operations, or removing unwanted characters.

  • Use list comprehension to modify the columns list.
Python
df = pd.read_csv('data.csv') df.columns = [col.upper() for col in df.columns] print(df) 

Output:

Screenshot-2024-12-31-123629
Output

In this case, all column names are converted to uppercase. This method is highly customizable and allows us to apply conditions like removing spaces, changing the case, or applying regular expressions.

Method 6: Renaming Columns by Replacing Specific Characters

If we need to replace specific characters or patterns in column names, we can use str.replace(). This method is perfect for cleaning up column names, such as removing spaces or replacing special characters.

  • Use str.replace() to rename columns.
Python
df = pd.read_csv('data.csv') df.columns = df.columns.str.replace(' ', '_') print(df) 

Output:

Screenshot-2024-12-31-123824
Output

In this example, spaces are replaced with underscores. It's particularly useful for cleaning up messy or inconsistent column names.

Method 7: Renaming Columns by Mapping Functions

We can map a function to the column names to rename them according to a custom rule. This method is highly flexible and can be used to apply transformations such as converting names to lowercase, capitalizing the first letter, or applying any custom function to column names.

  • Use a mapping function like str.lower() or any custom function.
Python
df = pd.read_csv('data.csv') df.columns = df.columns.map(lambda x: x.lower()) print(df) 

Output:

Screenshot-2024-12-31-124029
Output

Using map() with a lambda function allows us to apply a custom transformation to column names. In this example, all column names are converted to lowercase.

Related Articles:

  • Rename column by index in Pandas
  • How to rename columns in Pandas DataFrame

Next Article
Pandas Rename Column

A

abhirajksingh
Improve
Article Tags :
  • Python
  • AI-ML-DS
  • Python-pandas
  • Python pandas-io
  • python
Practice Tags :
  • python
  • python

Similar Reads

    Rename column by index in Pandas
    A column of a data frame can be changed using the position it is in known as its index. Just by the use of the index, a column can be renamed. Dealing with large and complex datasets in Pandas often requires manipulating column names for better analysis. Renaming columns by their index position can
    6 min read
    Pandas Drop Column
    When working with large datasets, there are often columns that are irrelevant or redundant. Pandas provides an efficient way to remove these unnecessary columns using the `drop()` function. In this article, we will cover various methods to drop columns from a DataFrame.Pythonimport pandas as pd data
    4 min read
    How to rename columns in Pandas DataFrame
    In this article, we will see how to rename column in Pandas DataFrame. The simplest way to rename columns in a Pandas DataFrame is to use the rename() function. This method allows renaming specific columns by passing a dictionary, where keys are the old column names and values are the new column nam
    4 min read
    Pandas Dataframe Rename Index
    To rename the index of a Pandas DataFrame, rename() method is most easier way to rename specific index values in a pandas dataFrame; allows to selectively change index names without affecting other values. Pythonimport pandas as pd data = {'Name': ['John', 'Alice', 'Bob', 'Eve'], 'Age': [25, 30, 22,
    3 min read
    Python | Pandas Dataframe.rename()
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas rename() method is used to rename any index, column or row. Renaming of column
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences