rename column in pandas dataframe

How to Rename a Column in Pandas DataFrame: Rename Column Names (With Examples)

While working with data you may require to change the names of some or all the columns of a dataframe. In this tutorial, we’ll cover three different ways in Pandas to rename column names along with examples.

rename column in pandas dataframe

You can use the Pandas dataframe rename() function to rename column names in Pandas. There are other methods for renaming columns as well. The following are some easy, useful, and effective ways to change the name of columns in Pandas –

Table of Contents

How to Rename a Column in Pandas

Top 3 Methods to Rename Column Names in Panda

Summary: Changing Column Names in Pandas (Key Takeaways)

Top 3 Methods to Rename Column Names in Pandas

  • Method 1: Use the Pandas dataframe rename() function to modify specific column names.
  • Method 2: Use the Pandas dataframe set_axis() method to change all your column names.
  • Method 3: Set the dataframe’s columns attribute to your new list of column names.

Let’s explore in more detail by examing practical examples of using Pandas rename column names methods to manipulate and manage columns for data.

The Pandas dataframe rename() function is a quite versatile function used not only to rename column names but also row indices.

You can use this function to rename specific columns. Note that we can also rename a single column or multiple columns. The following is the syntax to change column names using the Pandas rename() function.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

df.rename(columns={"OldName":"NewName"})

The rename() function returns a new dataframe with renamed axis labels (i.e. the renamed columns or rows depending on usage). To modify the dataframe in place set the argument inplace to True.

Let’s now look at some examples.

Example: Change the name of a specific column

Here, we will create a dataframe storing the category and color information of some pets in the columns “Category” and “Color” respectively.

import pandas as pd

# data of pets
data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'],
       'Color': ['brown', 'black', 'white', 'green']}

# create pandas dataframe
df = pd.DataFrame(data)

# display the dataframe
df

Output:

pets dataframe with "Category" and "Color" columns

We now have a dataframe containing the “Category” and “Color” information of some pets.

Let’s now use the rename() function to change the name of the “Category” column to “Pet”.

# change column name "Category" to "Pet"
df = df.rename(columns={"Category":"Pet"})
# display the dataframe
df

Output:

pets dataframe with "Category" column name changed to "Pet"

You can see that the column name has been changed.

Note that you can also just get the column names of a Pandas dataframe using df.columns.

Example: Apply rename() function to column names

The rename() function also accepts a function that can be applied to each column name. For example, you can write a function to replace special characters like underscore _ with a space, split the column name on a delimiter, keep only a specific portion, etc.

import pandas as pd

# create a dataframe
data = {'Col1_Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'],
       'Col2_Color': ['brown', 'black', 'white', 'green']}
df = pd.DataFrame(data)

# print dataframe columns
print("Dataframe columns:", df.columns)

# change column names to the string after the _
df = df.rename(columns=lambda x: x.split("_")[1])

# print dataframe columns
print("Dataframe columns:", df.columns)

Output:

Dataframe columns: Index(['Col1_Category', 'Col2_Color'], dtype='object')
Dataframe columns: Index(['Category', 'Color'], dtype='object')

In the above example, we pass a function to the rename function to modify the column names. The function gets applied to each column and gives its respective new name. Here, we split each column name on _ and use the second string as our new name for that column.

The Pandas dataframe set_axis() method can be used to rename a dataframe’s columns by passing a list of all columns with their new names.

Note that the length of this list must be equal to the number of columns in the dataframe. The following is the syntax:

df.set_axis(new_column_list, axis=1)

You have to explicitly specify the axis as 1 or 'columns' to update column names since its default is 0 (which modifies the axis for rows).

It returns a new dataframe with the updated axis. To modify the dataframe in place, set the argument inplace to True.

Let’s now look at an example.

Example: Change the name of a column using set_axis()

We’ll take the same use case as above, and change the column name “Category” to “Pet” in a dataframe but this time we will be using the set_axis() method.

import pandas as pd

# data of pets
data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'],
       'Color': ['brown', 'black', 'white', 'green']}

# create pandas dataframe
df = pd.DataFrame(data)

# print dataframe columns
print("Dataframe columns:", df.columns)

# change column name Category to Pet
df = df.set_axis(["Pet", "Color"], axis=1)

# print dataframe columns
print("Dataframe columns:", df.columns)

Output:

Dataframe columns: Index(['Category', 'Color'], dtype='object')
Dataframe columns: Index(['Pet', 'Color'], dtype='object')

In the above example, the set_axis() function is used to rename the column Category to Pet in the dataframe df. Note that we had to provide the list of all the columns for the dataframe even if we had to change just one column name.

You can also update a dataframe’s column by setting its columns attribute to your new list of columns. The following is the syntax:

df.columns = new_column_list

Note that new_column_list must be of the same length as the number of columns in your dataframe.

Let’s look at our final example.

Example: Update columns attribute to change column names

We’ll take the same use case as above. Create a dataframe with “Category” ad “Color” columns and then change the column name “Category” to “Pet” but this time we’ll do it by updating the columns attribute.

import pandas as pd

# data of pets
data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'],
       'Color': ['brown', 'black', 'white', 'green']}

# create pandas dataframe
df = pd.DataFrame(data)

# print dataframe columns
print("Dataframe columns:", df.columns)

# change column name Category to Pet
df.columns = ["Pet", "Color"]

# print dataframe columns
print("Dataframe columns:", df.columns)

Output:

Dataframe columns: Index(['Category', 'Color'], dtype='object')
Dataframe columns: Index(['Pet', 'Color'], dtype='object')

In the above example, we change the column names of the dataframe df by setting df.columns to a new column list. Like the set_index() function, we had to provide the list of all the columns for the dataframe even if we had to change just one column name.

Summary: Changing Column Names in Pandas (Key Takeaways)

In this tutorial, we looked at three different methods along with examples of application to rename columns in a Pandas dataframe. The following are the key takeaways –

  • You can change Pandas dataframe column names by using the rename() function to change the name of a single column or to rename multiple columns. Pass a dictionary {"OldName":"NewName"} to the columns parameter. You can also pass a function to the columns parameter to dynamically change the column names.
  • You can set the column names of the dataframe to new column names using the Pandas dataframe setaxis() function. Explicitly specify axis=1 or axis='columns' to indicate that you’re setting the columns’ axis.
  • Alternatively, you can change the column names of a dataframe by changing the .columns attribute of the dataframe.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top