How to rename your dataframe columns in Pandas?

As part of your data preparation, you might want to modify your raw data column heading labels to allow for more descriptive analysis and visualization. In this article we’ll go through several scenarios in which you’ll be using Python and the Pandas library in order to manipulate your column headers.

As we typically do, we’ll start by creating our learning data and then look into several scenarios in which we’ll modify our dataframe headings:

  • Renaming a single column
  • Renaming multiple columns headings
  • Replace column names with a list

Creating our dataframe

Use the following code to quickly generate our test data for this tutorial.

#Python3 
#import the pandas library
import pandas as pd

# create our learning dataframe
sales = pd.DataFrame({"Year": [2018, 2018, 2019, 2019, 2020, 2020] ,
                     "Quarter": ["Q1", "Q2", "Q4", "Q3", "Q1", "Q4"],
                     "Sales Person": ["Daniel", "John", "Kim", "Kim", "Earl", "John"],
                     "Sales": [4500, 4800, 10000, 7000, 8000, 10000]})
sales.head()

Here’s how your data will look like:

YearQuarterSales PersonSales
02018Q1Daniel4500
12018Q2John4800
22019Q4Kim10000
32019Q3Kim7000
42020Q1Earl8000

Rename a single column

We’ll first tweak the Sales Person column header. Well use the dataframe rename method to pass a dictionary with the corresponding matching column name. The inplace parameter ensure that the change to the header name is permanent.

# Rename single column
sales.rename(columns = {"Sales Person":"Account Manager"}, inplace="True")
sales.head(1)

The result will look as following:

YearQuarterAccount ManagerSales
02018Q1Daniel4500

Rename multiple headings

Next, will modify several headers at the same time. As we are passing the column names in a dictionary, we can pass several key/value pairs in one shot as shown in the short script below.

# Rename multiple columns
sales.rename(columns = {"Sales Person":"Account Manager", "Sales":"Sales (USD)"}, inplace="True")
sales.head(1)

Here’s the result:

YearQuarterAccount ManagerSales (USD)
02018Q1Daniel4500

Change column names to list values

Another useful technique is to modify the dataframe columns property directly. In this example we’ll pass a list containing the relevant column names.

# Replace all column names with list values
sales.columns = ["Fiscal Year", "Fiscal Quarter","Account Manager", "Sales"]
sales.head(1)
Fiscal YearFiscal QuarterAccount ManagerSales
02018Q1Daniel4500

Leave a Comment