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.

#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]})

Here’s how your data will look like:

YearQuarterSales PersonSales

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")

The result will look as following:

YearQuarterAccount ManagerSales

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")

Here’s the result:

YearQuarterAccount ManagerSales (USD)

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"]
Fiscal YearFiscal QuarterAccount ManagerSales

Leave a Comment