Table of contents
1.
Introduction
2.
What is the Pandas DataFrame?
3.
How to Rename Columns in Pandas DataFrame?
4.
Method 1: Rename Specific Columns
5.
Method 2: Rename Multiple Columns
6.
Method 3: Rename All Columns
7.
Method 4: Replace column name using Dataframe.columns.str.replace function
8.
Method 5: Replace column name using Dataframe add_prefix() and add_suffix() function
9.
Method 6: Replace column name using Dataframe set_axis() function
10.
Frequently Asked Questions
10.1.
How do I rename a column in Pandas? 
10.2.
How do I select and rename columns in Pandas? 
10.3.
How to rename column name Pandas using list? 
11.
Conclusion
Last Updated: Mar 27, 2024
Medium

How To Rename Columns In Pandas (With Examples)

Author Manan Singhal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Pandas DataFrame is a rectangular grid for storing data. When data is saved in a data frame, it is simple to visualize and manipulate. Each row is a measurement of an instance, whereas each column represents a vector containing data for a certain attribute/variable. Each Dataframe column contains homogeneous data across each column, whereas Dataframe rows can contain homogeneous or heterogeneous data throughout each row. Pandas Dataframe axes are labeled, unlike two-dimensional arrays. In this article, we will discuss the Pandas rename column, but before that let’s find out what Pandas DataFrame actually is.

pandas rename column

What is the Pandas DataFrame?

A Pandas DataFrame is a data structure in Python that helps to analyze and organize the data in a tabular format. It is part of the pandas library that is used for Data manipulation and analysis. It organizes the data similar to a table in a spreadsheet.

For example, we have a collection of cards, and each card has different details written on it such as a person's name, age, and favorite color. A data frame is like a special card holder that arranges all the cards in rows and columns, so we can see all the information.

With a DataFrame, we can sort the cards based on someone's age, find out how many people like the same color, or add new cards to the collection.

How to Rename Columns in Pandas DataFrame?

In Pandas, there are multiple methods available to rename columns in a DataFrame. Here are six commonly used methods, along with a detailed explanation of each:

Method 1: Rename Specific Columns

The rename() function can rename columns in a Pandas Dataframe. We can use this method because we only need to supply information for the columns that need to be renamed.

Let's see the implementation of how to rename columns in Pandas DataFrame using rename-specific columns.

Code

# Python Code: How to rename columns in Pandas DataFrame
import pandas as pandas

# creating a dictionary
student = {
  'name': ['Aman', 'Ayush', 'Harsh', 'Rahul', 'Rohit'],
  'entry': ['1', '2', '3', '4', '5'], 
  'code': ['95', '81', '73', '94', '15']
  }
  
student_pandas_dataframe = pandas.DataFrame(student)
print(student_pandas_dataframe.columns)

# using rename to change column name
student_pandas_dataframe.rename(columns = {'code':'marks'}, inplace = True)
print(student_pandas_dataframe.columns)
You can also try this code with Online Python Compiler
Run Code


Output

Output

Method 2: Rename Multiple Columns

The rename() function can rename columns in a Pandas Dataframe. When we need to rename multiple columns, we can use this method because we only need to supply information for the columns that need to be renamed.

Let's see the implementation of how to rename columns in Pandas DataFrame using rename in multiple columns.

Code

# Python Code: How to rename columns in Pandas DataFrame
import pandas as pandas

# creating a dictionary
student = {
  'name': ['Aman', 'Ayush', 'Harsh', 'Rahul', 'Rohit'],
  'entry': ['1', '2', '3', '4', '5'], 
  'code': ['95', '81', '73', '94', '15']
  }
  
student_pandas_dataframe = pandas.DataFrame(student)
print(student_pandas_dataframe.columns)

# using rename to change column name
student_pandas_dataframe.rename(columns = {'name': 'Name', 'code':'Marks'}, inplace = True)
print(student_pandas_dataframe.columns)
You can also try this code with Online Python Compiler
Run Code

 

Output

Output

Method 3: Rename All Columns

Columns can also be renamed by explicitly setting a list containing the new names to the columns attribute of the Dataframe object whose columns we want to rename. The disadvantage of using this is that we must provide new names for all columns, even if we only wish to change a subset of them.

Let's see the implementation of how to rename columns in Pandas DataFrame using renaming all columns at once.

Code

# Python Code: How to rename columns in Pandas DataFrame
import pandas as pandas

# creating a dictionary
student = {
  'name': ['Aman', 'Ayush', 'Harsh', 'Rahul', 'Rohit'],
  'entry': ['1', '2', '3', '4', '5'], 
  'code': ['95', '81', '73', '94', '15']
  }
  
student_pandas_dataframe = pandas.DataFrame(student)
print(student_pandas_dataframe.columns)

# using rename to change column name
student_pandas_dataframe.columns = ['Name', 'Entry', 'Marks']
print(student_pandas_dataframe.columns)
You can also try this code with Online Python Compiler
Run Code

 

Output

Output

Method 4: Replace column name using Dataframe.columns.str.replace function

We will use the replace function to rename the column name, passing the old and new names as parameters to the column.

Let's see the implementation of how to rename columns in Pandas DataFrame using columns.str.replace method in DataFrame.

Code

# Python Code: How to rename columns in Pandas DataFrame
import pandas as pandas

# creating a dictionary
student = {
  'name': ['Aman', 'Ayush', 'Harsh', 'Rahul', 'Rohit'],
  'entry': ['1', '2', '3', '4', '5'], 
  'code': ['95', '81', '73', '94', '15']
  }
  
student_pandas_dataframe = pandas.DataFrame(student)
print(student_pandas_dataframe.columns)

# replace column name
student_pandas_dataframe.columns = student_pandas_dataframe.columns.str.replace('name', 'Name')
student_pandas_dataframe.columns = student_pandas_dataframe.columns.str.replace('entry', 'Entry')
student_pandas_dataframe.columns = student_pandas_dataframe.columns.str.replace('code', 'Marks')
print(student_pandas_dataframe.columns)
You can also try this code with Online Python Compiler
Run Code

 

Output

Output

Method 5: Replace column name using Dataframe add_prefix() and add_suffix() function

We will rename the column name by passing the prefix and suffix that should be added to the first and last name of the column name to the add Sufix and add Prefix functions.

Let's see the implementation of how to rename columns in Pandas DataFrame using the add_prefix() and add_suffix() method in DataFrame.

Code

# Python Code: How to rename columns in Pandas DataFrame
import pandas as pandas

# creating a dictionary
student = {
  'name': ['Aman', 'Ayush', 'Harsh', 'Rahul', 'Rohit'],
  'entry': ['1', '2', '3', '4', '5'], 
  'code': ['95', '81', '73', '94', '15']
  }
  
student_pandas_dataframe = pandas.DataFrame(student)
print(student_pandas_dataframe.columns)

# replace column name
student_pandas_dataframe = student_pandas_dataframe.add_prefix('P_')
student_pandas_dataframe = student_pandas_dataframe.add_suffix('_S')
print(student_pandas_dataframe.columns)
You can also try this code with Online Python Compiler
Run Code

 

Output

Output

Method 6: Replace column name using Dataframe set_axis() function

We will rename the column name using the set axis method, passing as parameters the new column name and the axis that should be replaced with a new name in the column.

Let's see the implementation of how to rename columns in Pandas DataFrame using the set_axis() method in DataFrame.

Code

# Python Code: How to rename columns in Pandas DataFrame
import pandas as pandas

# creating a dictionary
student = {
  'name': ['Aman', 'Ayush', 'Harsh', 'Rahul', 'Rohit'],
  'entry': ['1', '2', '3', '4', '5'], 
  'code': ['95', '81', '73', '94', '15']
  }
  
student_pandas_dataframe = pandas.DataFrame(student)
print(student_pandas_dataframe.columns)

# replace column name
student_pandas_dataframe.set_axis(['Name', 'Entry', 'Marks'], axis='columns', inplace=True)
print(student_pandas_dataframe.columns)
You can also try this code with Online Python Compiler
Run Code

 

Output

Output

Frequently Asked Questions

How do I rename a column in Pandas? 

To rename a column in Pandas, you can use the 'rename()' method, provide a dictionary where the current column name is the key, and the new column name is the value. This will update the column name in the DataFrame, making it easier for you to work with and understand your data.

How do I select and rename columns in Pandas? 

To select and rename columns in Pandas, use the indexing operator [] to choose the columns and then use the rename() method to give them new names using a dictionary. This approach allows to customize the column names to represent better the data they hold.
 

How to rename column name Pandas using list? 

To rename column names in Pandas using a list, you can directly assign the new list of column names to the ‘columns’ attribute of the DataFrame. This will update the column names accordingly. This will update the column names to something more meaningful or descriptive based on your needs.
 

Conclusion

In the article, we have discussed how to rename columns in Pandas DataFrame. We hope that this article will help you understand the concept of the pandas, and if you want to learn more about it, check out our other blogs on this topic:

Happy Coding!

Live masterclass