How to rename one or more Python Pandas DataFrame columns?

When working with tabular data that was acquired from different sources, you might need to tidy it up by modifying column names. In today’s tutorial we’ll learn to use the DataFrame.rename() method of the Pandas library which as you can infer, helps renaming both rows and columns index in your DataFrame.

Create Example DataFrame

Let us start by creating a simple DataFrame:

import pandas as pd

office_dict = {'CTY': ['New York', 'Boston', 'Atlanta','New York'],
             'OFC': [1,2,1,2],
              'CAND': [100,150,100, 120]}

hr = pd.DataFrame(office_dict)

Let’s look at our DataFrame column index:

Index(['CTY', 'OFC', 'CAND'], dtype='object')

Rename a single column by label

The simplest case is to rename a single column. We’ll pass a simple mapper dictionary to the the DataFrame.rename() method. The mapper consist of key / value pairs of the current and the new name. Note that if a column label is not included in the mapper then its label won’t be replaced. Here’s an example:

hr.rename(columns = {'CAND':'num_candidates'})

We’ll get the following output:

Important note: If you’ll look at the columns index, you might be wondering why Pandas rename is not working for you. Remember that If you want to persist the column name changes you need to add the inplace=True as a parameter when calling rename().

hr.rename(columns = {'CAND':'num_candidates'}, inplace=True)

Rename single column by index

We can rename one or multiple columns by using their index. Here’s an example:

hr.rename(columns = {hr.columns[1]: 'office'})

Change a Pandas Series name

In a similar fashion we can modify the name of a Pandas Series.

In the following example we’ll first create a Series by slicing a DataFrame column, then use the Series.rename() method.


Rename multiple DataFrame columns

Renaming multiple column labels is similar, we’ll just pass the current and new values as key value pairs as a mapper.

hr.rename (columns = {'CTY':'city', 'CAND':'num_candidates', 'OFC': 'office'},inplace=True)

Here’s our DataFrame:

Modify the row index labels with a list

Another case is that you might need to easily replace the row index values. Note that in this example we pass the mapping dictionary to the index parameter.

# Define two lists - the current index and a new index
curr_idx = list(hr.index)
new_idx = [i+1 for i in hr.index]

# Use zip() to join the two lists 
hr.rename (index = dict(zip(curr_idx, new_idx)))