7 Tips & Tricks to Rename Column in Pandas DataFrame

In this tutorial, we will show you various ways how to rename column in Pandas dataframe. In specific we will cover three main approaches of columns attribute, set_axis(), and rename() function for pandas rename of columns along with examples.

Create a Sample Pandas DataFrame

Let us create a sample dataframe in pandas that will be used in all the subsequent examples to change column name in Pandas.

In [0]:

import pandas as pd;

# create a dataframe
data = {'Employee_No': ['E0001', 'E0002', 'E0003', 'E0004'],
'Employee_Name': ['John', 'Ron', 'Sam', 'Alice'],
'Department':['Sales','Sales','Finance','Finance']}
df = pd.DataFrame(data)
df

Out[0]:

Employee_No Employee_Name Department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Change Column Names in Pandas with Column Attribute

Example 1

The first way to change the column name in pandas is to use the column attribute of the datframe. As shown in the below example, we have assigned the list of columns with modified names to df.column attribute.

It should be noted here that even though we don’t want to change the names of all columns, it is mandatory to pass all names of columns. Otherwise, it will throw an error.

In the below example we wanted to rename Employee_Id to Emp_Id and Emloyee_Name to Emp_Name. But still, we passed the Department column even though it was unchanged.

In [1]:

df.columns = ["Emp_Id", "Emp_Name", "Department"]
df

Out [1]:

Emp_Id Emp_Name Department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Change Column Names in Pandas with set_axis()

Example 2

In this example, we shall use set_axis attribute of Pandas to modify the column name. Just like the previous approach, here also we have to pass the entire list of columns even though we want to change just the first two columns.

The axis = 1 tells pandas that the changes are to be applied on the columns and also inplace = True is used so that changes are persisted in the current dataframe df.

In [2]:

df.set_axis(["Emp_Id", "Emp_Name", "Department"], axis=1, inplace = True)
df

Out [2]:

Emp_Id Emp_Name Department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Rename Column Names in Pandas with rename() 

In the earlier examples with column and set_axis attributes, we had to pass the entire list of columns even though we had a requirement to change an only couple of them. It is okay when the total number of columns is less but when there are hundreds of columns then it is not practical to pass all column names just to change a couple of them.

This is where rename() comes in very handy as it lets you work with only the column that needs to be renamed in pandas. And it also supports the use of the lambda function to let you modify the columns programmatically.  Let us understand this with various examples.

Example 3

In this example, we are passing the mapping of existing and new column names in the dictionary data structure to rename() function. It should be noted that here only those columns are passed that are to be renamed. Also, the axis =1 tells Pandas that this rename is for columns and inplace=True allows the change to persist in place in the dataframe.

In [3]:

df.rename({'Employee_No':'Emp_Id', 'Employee_Name':'Emp_Name'}, axis=1, inplace = True)
df

Out [3]:

Emp_Id Emp_Name Department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Example 4

This is another variation of the above example of columns rename in Pandas. Instead of passing axis=1 we are using columns parameter to assign the dictionary mapping of old and new column names.

In [4]:

df.rename(columns= {'Employee_No':'Emp_Id', 'Employee_Name':'Emp_Name' }, inplace = True)
df

Out [4]:

Emp_Id Emp_Name Department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Example 5 – Rename Column Names to Lower Case in Pandas

It is quite easy to rename all columns of pandas dataframe to lower case with rename() function as shown in the below example. Isn’t it cool?

In [5]:

df.rename(columns= str.lower, inplace = True)
df

Out [5]:

employee_no employee_name department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Example 6 – Change Column Names to Upper Case in Pandas

Just like the above example, we can also change column names to upper cases with rename() function as shown below.

In [6]:

df.rename(columns= str.upper, inplace = True)
df

Out[6]:

EMPLOYEE_NO EMPLOYEE_NAME DEPARTMENT
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

Example 7 – Rename Pandas Column Names with Lambda Function

If you wish to change rename columns of pandas dataframe programmatically it can be done by rename() by using lambda function. This really adds to the flexibility and power that rename() function offers.

In our example, we wish to add the prefix ‘Cmp_’  in front of every column name. Rather than doing this manually, we have done this with the lambda function as shown below.

Lambda function opens the whole new possibilities of renaming columns as you want just with a line of code.

In [7]:

df.rename(columns=lambda x: 'Cmp_'+ x, inplace = True)
df

Out[7]:

Cmp_Employee_No Cmp_Employee_Name Cmp_Department
0 E0001 John Sales
1 E0002 Ron Sales
2 E0003 Sam Finance
3 E0004 Alice Finance

 

 

Reference – Pandas Documentation

Follow Us

Leave a Reply

Your email address will not be published. Required fields are marked *