Geekflare is supported by our audience. We may earn affiliate commissions from buying links on this site.
In Development Last updated: June 26, 2023
Share on:
Invicti Web Application Security Scanner – the only solution that delivers automatic verification of vulnerabilities with Proof-Based Scanning™.

Renaming columns in a pandas dataframe is a common operation. Learn the four different methods to rename pandas columns.

Pandas is a popular Python library for data analysis. Data that we need to analyze is often available in different formats, including csv and tsv files, relational databases, and more. And you need to do some preliminary checks on the data, handle missing values, and prepare the data for further analysis.

With pandas, you can:

  • Ingest data from various sources 
  • Explore the dataset and handle missing values in it
  • Analyze the dataset to gain insights

For all data analysis projects, you’ll often create data frames from Python data structures such as a dictionary. Or you’ll read data from other sources, such as a csv file into a data frame.

The dataframe is the basic data structure in pandas. It contains the records along the rows and the various fields or attributes along the columns.

However, you may have to rename the column names—to make them more descriptive and improve readability. Here, you’ll learn four different ways to rename columns. Let’s begin!

Creating a Pandas DataFrame

You can follow along with the tutorial in a Jupyter notebook environment with pandas installed. Or you can follow along on Google Colab.

First, we’ll create a pandas dataframe and work with it for the remainder of the tutorial.

Here’s a dictionary book_dict:

books_dict = {
    "one": [
        "Atomic Habits",
        "His Dark Materials",
        "The Midnight Library",
        "The Broken Earth",
        "Anxious People",
    ],
    "two": [
        "James Clear",
        "Philip Pullman",
        "Matt Haig",
        "N.K.Jemisin",
        "Fredrik Backman",
    ],
    "three": ["Nonfiction", "Fantasy", "Magical Realism", "Fantasy", "Fiction"],
    "four": [4, 5, 3, 5, 4],
}

We’ll first import pandas and then create a dataframe df from books_dict.

import pandas as pd

Note: We’ll keep coming back to the following code cell—to create an initial version of the dataframe—before renaming the columns.

df = pd.DataFrame(books_dict)

We can use df.head() to get the first few rows of the dataframe df. By default, it returns the first five rows. Here the df has only five rows; so when using df.head() we get the entire dataframe.

df.head()
image-230

We see that the column names are currently the keys of the dictionary. But this is not very descriptive. So let’s rename them! 👩‍🏫

Methods to Rename Columns in Pandas

Now let’s look at the various methods to rename columns in pandas:

  • Setting the columns attribute of the dataframe to the list of new column names
  • Using the rename() method on the dataframe
  • Using str.replace to rename one or more columns
  • Using the set_axis() method on the dataframe

Setting the columns Attribute

For any dataframe, the columns attribute contains the list of column names:

df.columns
# Index(['one', 'two', 'three', 'four'], dtype='object')

Let’s rename the columns to denote what each field stands for and then call df.head() to see the results:

df.columns = ['Title','Author','Genre','Rating']
df.head()
image-231

Using the rename() Method

To rename columns in pandas, you can use the rename() method with the syntax:

df.rename(column={mapping})

This mapping can be a dictionary that is of the following form:

{'old_col_name_1':'new_col_name_1', 'old_col_name_2':'new_col_name_2',...,
'old_col_name_n':'new_col_name_n'}

Let’s create df from the books_dict dictionary:

df = pd.DataFrame(books_dict)

Using the rename() method with the above syntax, we get df_1. Which is a copy of the dataframe with the columns renamed.

df_1 = df.rename(columns={'one':'Title','two':'Author','three':'Genre','four':'Rating'})
df_1.head()

So the column names of df_1 are modified:

image-232

But the column names of the original dataframe df do not change:

df.head()
image-233

Because this method lets us provide a mapping between the old and the new column names, we can use it to rename both single and multiple columns.

Rename Columns in Place

What if you want to modify the existing data frame—without creating a new copy?

To do this you can set inplace equal to True in the method call.

df.rename(columns={'one':'Title','two':'Author','three':'Genre','four':'Rating'},inplace=True)
df.head()

This will rename the columns of the original dataframe df:

rename-pandas-columns

So far we have seen how to:

  • Rename columns by providing a dictionary that maps the old column names to the new column names 
  • Rename columns in place without creating a new dataframe

You can use the rename method in another way, too.

Another Approach to Renaming Columns

Let’s rename the columns so that they are in uppercase:

df = pd.DataFrame(books_dict)
df.columns = ['TITLE','AUTHOR','GENRE','RATING']
df.head()

The dataframe df now looks like this:

image-234

Suppose we want to change each of these column names to be in the title case. Instead of providing a dictionary for each column name, we can specify a function or method call on an object as shown:

df.rename(str.title,axis='columns',inplace=True)
df.head()
rename-pandas-columns

Here, we set axis  to 'columns' and use str.title to convert all the column names to the title case.

Using str.replace() on the Column Name Strings

As always, run the following code cell to create the dataframe from the dictionary:

df = pd.DataFrame(books_dict)

In Python, you’d have used the replace() method with the syntax str.replace(this, with_this) to get a copy of a string with the required changes. Here’s an example:

>>> str1 = 'Marathon'
>>> str1.replace('Mara','Py')
'Python'

You know that the columns attribute contains a list of strings containing the names of the columns. So you can call str.replace('old_column_name','new_column_name') like so:

df.columns = df.columns.str.replace('one','Title')
df.head()
rename-pandas-columns

Here we renamed only the column ‘one’ to ‘Title’, so the other column names remain unchanged.

Now let’s rename the other columns using the same approach:

df.columns = df.columns.str.replace('two','Author')
df.columns = df.columns.str.replace('three','Genre')
df.columns = df.columns.str.replace('four','Rating')
df.head()
image-232

This method of renaming columns is helpful when you need to rename only one or a small subset of the columns.

Using the set_axis() Method

Let’s go back to the initial version of a dataframe:

df = pd.DataFrame(books_dict)

You can also use the set_axis() method to rename the columns. The syntax is as follows:

df.set_axis([list_of_column_names],axis='columns')

By default the set_axis() method returns the copy of the dataframe. But if you want to modify the dataframe in place, you can set copy to False.

df = df.set_axis(['Title','Author','Genre','Rating'],axis='columns',copy=False)
df.head()
rename-pandas-columns

Conclusion

Here’s a review of the different methods to rename columns in a pandas dataframe:

  • For a sample dataframe df, the columns attribute df.columns is the list of column names. To rename the columns, you can set this attribute to the list of new column names.
  • The rename() method to rename columns works with the syntax: df.rename(columns={mapping}) where mapping refers to the mapping from the old column names to the new column names. You can also use the rename() method by specifying a function to be applied to all the column names: df.rename(function or method call, axis='columns').
  • Just the way you use the replace() method on a Python string, you can use df.columns.str.replace('old_column_name', 'new_column_name') to replace column names. 
  • Another approach to rename in columns is to use the set_axis method with the syntax: df.set_axis(list_of_col_names,axis='columns').

That’s all for this tutorial! Check out the list of collaborative notebooks for data analysis.

  • Bala Priya C
    Author
    Bala Priya is a developer and technical writer from India with over three years of experience in the technical content writing space. She shares her learning with the developer community by authoring tech tutorials, how-to guides, and more…. read more
  • Narendra Mohan Mittal
    Editor

    Narendra Mohan Mittal is a Senior Digital Branding Strategist and Content Editor with over 12 years of versatile experience. He holds an M-Tech (Gold Medalist) and B-Tech (Gold Medalist) in Computer Science & Engineering.


    read more
Thanks to our Sponsors
More great readings on Development
Power Your Business
Some of the tools and services to help your business grow.
  • Invicti uses the Proof-Based Scanning™ to automatically verify the identified vulnerabilities and generate actionable results within just hours.
    Try Invicti
  • Web scraping, residential proxy, proxy manager, web unlocker, search engine crawler, and all you need to collect web data.
    Try Brightdata
  • Monday.com is an all-in-one work OS to help you manage projects, tasks, work, sales, CRM, operations, workflows, and more.
    Try Monday
  • Intruder is an online vulnerability scanner that finds cyber security weaknesses in your infrastructure, to avoid costly data breaches.
    Try Intruder