Pandas – Rename Column Names

While working with data it may happen that you require to change the names of some or all the columns of a dataframe. Whether you’re changing them to correct a typo or simply to give columns more readable names, it’s quite handy to know how to quickly rename columns. In this tutorial, we’ll cover some of the different ways in pandas to rename column names along with examples.

To rename columns of a dataframe you can –

  • Use the pandas dataframe rename() function to modify specific column names.
  • Use the pandas dataframe set_axis() method to change all your column names.
  • Set the dataframe’s columns attribute to your new list of column names.

The pandas dataframe rename() function is a quite versatile function used not only to rename column names but also row indices. The good thing about this function is that you can rename specific columns. The syntax to change column names using the rename function is –

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.

Example 1: Change names of a specific column

import pandas as pd

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

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

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

# 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 dataframe df is created with columns: Category and Color. The rename() function is then used to change the column name Category to Pet which returns a new dataframe which is saved to df.

Example 2: Apply function to column names

The rename() function also accepts function that can be applied to each column name.

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 the column name on _ and use the second string as our new 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.

Example: Change column names using set_axis

import pandas as pd

# create a dataframe
data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'],
       'Color': ['brown', 'black', 'white', 'green']}
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 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 they syntax:

df.columns = new_column_list

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

Example: Change column name by updating the columns attribute.

import pandas as pd

# create a dataframe
data = {'Category': ['Dog', 'Cat', 'Rabbit', 'Parrot'],
       'Color': ['brown', 'black', 'white', 'green']}
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 columns for the dataframe even if we had to change just one column name.

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.