Rename Columns in Pandas DataFrame: Explained

In this tutorial, you will learn how to rename columns in Pandas DataFrame.

In a nutshell, Pandas is a popular data analysis and manipulation library in Python. It provides powerful tools for reading, writing, filtering, and transforming data in a variety of formats, such as CSV, Excel, and SQL databases. If you want to learn how to rename columns in Dataframe Pandas, then this tutorial will help you choose the best method according to your needs.

In this mini tutorial, we will review four methods that will help you rename single or multiple-column names. In the end, frequently asked questions have been answered, so if you have a question that is not addressed in the tutorial, then do visit the end section.

rename() method of Pandas DataFrame

The simplest of all ways to rename columns in Pandas DataFrame is to use Pandas’ built-in method rename(). Consider the following example

import pandas as pd 

# create a sample DataFrame 
df=pd.DataFrame({'Name':['Alice','Bob','Charlie','David'], 
                 'Age':[25, 30, 35, 40], 
                 'City':['New York','Paris','London','Sydney']}) 

# rename the 'City' column to 'Location' 
df=df.rename(columns={'City': 'Location'}) 

print(df)
      Name  Age  Location
0    Alice   25  New York
1      Bob   30     Paris
2  Charlie   35    London
3    David   40    Sydney

As you can see, the column named 'City' was successfully renamed to 'Location'. You can also rename multiple columns by specifying a dictionary that maps old column names to new column names:

# rename multiple columns 
df=df.rename(columns={'City': 'Location', 'Age': 'AgeGroup'}) 

print(df)
      Name  AgeGroup  Location
0    Alice        25  New York
1      Bob        30     Paris
2  Charlie        35    London
3    David        40    Sydney

Rename the index and columns together

If you want to rename both the index and columns in a Pandas DataFrame together, you can use the rename method and pass two dictionaries: one for the index and one for the columns. The keys in each dictionary correspond to the old names, and the values correspond to the new names. This will allow you to rename column pandas by index.

import pandas as pd

# create a sample dataframe
df = pd.DataFrame({'Employee_Name':['Alice','Bob','Charlie','David'], 
                   'Employee_Age':[25, 30, 35, 40], 
                   'Employee_City':['New York','Paris','London','Sydney']},
                    index=['1', '2', '3', '4'])

# create dictionaries for renaming index and columns
index_dict = {"1": 'row_1', "2": 'row_2', "3": 'row_3', "4" : 'row_4'}
columns_dict = {'Employee_Name': 'Emp_Name', 'Employee_Age': 'Emp_Age', 'Employee_City': 'Emp_City'}

# rename index and columns using dictionaries
df = df.rename(index=index_dict, columns=columns_dict)

print(df)
      Emp_Name  Emp_Age  Emp_City
row_1    Alice       25  New York
row_2      Bob       30     Paris
row_3  Charlie       35    London
row_4    David       40    Sydney

columns an attribute of Pandas DataFrame

Another method to rename columns in Pandas DataFrame is by passing a list of new column names to the columns attribute of the DataFrame. For this method, you should be familiar with your data very well. As in this method, you will not be using any old names, so you need to ensure that the sequence of column names matches your data. Here’s an example:

import pandas as pd 

# create a sample dataframe 
df = pd.DataFrame({'Name':['Alice','Bob','Charlie','David'], 
                 'Age':[25, 30, 35, 40], 
                 'City':['New York','Paris','London','Sydney']}) 
# rename the columns using a list 
df.columns = ['EmployeeName', 'AgeGroup', 'Location'] 

print(df)
 EmployeeName  AgeGroup  Location
0        Alice        25  New York
1          Bob        30     Paris
2      Charlie        35    London
3        David        40    Sydney

Note that the length of the list should be equal to the number of columns in the DataFrame.

set_axis method of Pandas DataFrame

Another way to rename columns in Pandas DataFrame is to use the set_axis() method. The set_axis() method allows you to set the labels for a specified axis of a DataFrame or Series.

Here’s an example of how you can use the set_axis() method to rename the columns of a pandas DataFrame:

import pandas as pd 

# create a sample dataframe 
df = pd.DataFrame({'Name':['Alice','Bob','Charlie','David'], 
                 'Age':[25, 30, 35, 40], 
                 'City':['New York','Paris','London','Sydney']}) 

# rename the columns using set_axis() 
df.set_axis(['EmployeeName', 'AgeGroup', 'Location'] , axis=1, inplace=True) 

print(df)
  EmployeeName  AgeGroup  Location
0        Alice        25  New York
1          Bob        30     Paris
2      Charlie        35    London
3        David        40    Sydney

Here set_axis=1 specifies that we want to rename the columns, and inplace=True argument modifies the DataFrame in place, so there is no need to assign the result back to the original variable.

Note that set_axis() is a more general method that can be used to rename both the index and columns of a DataFrame or Series. So, you need to specify axis=1 to rename columns specifically.

Renaming using str.replace() method

Let’s say you have a pattern in your column names, and now you want to replace that pattern with something else, or you do not want to replace the whole column but just parts of it. Then this method will help you. Python method str.replace()  allows you to replace a specific substring with another substring within the column names of a DataFrame. Following is an example

import pandas as pd 

# create a sample dataframe 
df = pd.DataFrame({'Employee_Name':['Alice','Bob','Charlie','David'], 
                 'Employee_Age':[25, 30, 35, 40], 
                 'Employee_City':['New York','Paris','London','Sydney']}) 

# replace the 'Employee_' substring in the column names with 'Emp_' 
df.columns = df.columns.str.replace('Employee_', 'Emp_') 
print(df)
  Emp_Name  Emp_Age  Emp_City
0    Alice       25  New York
1      Bob       30     Paris
2  Charlie       35    London
3    David       40    Sydney

Keep in mind that this method will modify the original DataFrame, so if you want to keep the original DataFrame unchanged, you should create a copy of it first.

Conclusion

That’s the end of this tutorial. I hope that this tutorial has helped you understand how to rename columns in Pandas. Do practice these methods on your own. That’s how you will master these and will be applying them to different scenarios. If you have any query left that is not addressed in this tutorial or you got stuck in code, then do let me know in the comment section. I will be available. If you like this tutorial, then consider checking out our Python tutorials page, where we regularly post content for beginners and advanced developers. You’re sure to find something interesting there.