Rename Pandas Dataframe columns

Use cases for code example

Use case examples – rename Pandas DataFrame columns

When do we usually need to rename Pandas DataFrame columns? Here are some use case examples:

  • Rename to reflect the data captured in the column
    • A source dataset may have column names that are not meaningful
  • Standardise column names across multiple dataframes:
    • We may have multiple dataframes with columns that capture similar values¬† but their column names are different. We rename the column names to a standardised name to enable us to identify the common data relationship between dataframes. It will also allow us to easily merge the dataframesExample:
      Dataframe df captures customer ID in the column called ID
      Dataframe df1 also captures the same customer ID but in a column called IDENTITY
      As both the values in both dataframes refers to the same customer ID, we may want to rename the column to CustomerID to reflect the relationship and similarity

Sample Dataset for this example

You can follow the examples by using our dataset. Our dataset contains details of Sydney AirBnB listings as of 23 Jul 2017. (Source)

Walkthrough and explanation of code

Walkthrough of code – Rename Pandas DataFrame column – single and multiple columns

Using your Jupyter Python Notebook, we run this set of code to:

Example code

Example Code – Rename Pandas DataFrame Column – Single and Multiple columns

Using your Jupyter Python Notebook, run this set of code to load the dataset into a Pandas DataFrame

import pandas as pd

df_cdr = pd.read_csv('data//airbnb//sydney//tomslee_airbnb_sydney_1523_2017-07-23.csv')

Here’s a screenshot of the code and corresponding output:

Rename Pandas Dataframe column

Rename Pandas DataFrame columns – Single Column

In this example, we rename column room_type to airbnb_room_type

#rename Pandas DataFrame df_cdr column room_type in  to airbnb_room_type

df_cdr.rename({'room_type':'airbnb_room_type'}, axis='columns',inplace=True)

Rename single Pandas Dataframe Column

Rename Pandas DataFrame columns – Multiple Columns

Using the same dataframe, df_cdr, we rename 2 columns:

  • column name city to airbnb_city
  • column name neighborhood to airbnb_neighborhood
#renaming of multiple columns in the dataframe df_cdr

df_cdr.rename({'city':'airbnb_city', 'neighborhood':'airbnb_neighborhood' }, 

Rename multiple columns in Pandas Dataframe

Was this article helpful?

Related Articles