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 some of the different ways in Pandas to rename column names along with examples.
How to rename a column in pandas?

You can use the Pandas dataframe rename()
function to rename column names in Pandas. There are other methods as well. The following are some of the ways in which you can change the name of columns in Pandas –
- 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.
Using Pandas rename()
function
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. The following is the syntax to change column names using the Pandas rename()
function.
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.
Change the name of a specific column in Pandas dataframe
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:

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:

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
.
Apply function to column names in pandas dataframe
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.
Using Pandas set_axis()
function
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 some examples. 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 columns for the dataframe even if we had to change just one column name.
Changing the columns attribute
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 an example. 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 columns for the dataframe even if we had to change just one column name.
Summary – Change Column Name in Pandas
In this tutorial, we looked at some different methods to rename columns in a Pandas dataframe. The following are the key takeaways –
- Use the pandas dataframe
rename()
function to change the name of one or more columns. Pass a dictionary{"OldName":"NewName"}
to thecolumns
parameter. You can also pass a function to thecolumns
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 specifyaxis=1
oraxis='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
More on Pandas DataFrames –
- Pandas – Sort a DataFrame
- Change Order of Columns of a Pandas DataFrame
- Pandas DataFrame to a List in Python
- Pandas – Count of Unique Values in Each Column
- Pandas – Replace Values in a DataFrame
- Pandas – Filter DataFrame for multiple conditions
- Pandas – Random Sample of Rows
- Pandas – Random Sample of Columns
- Save Pandas DataFrame to a CSV file
- Pandas – Save DataFrame to an Excel file
- Create a Pandas DataFrame from Dictionary
- Convert Pandas DataFrame to a Dictionary
- Drop Duplicates from a Pandas DataFrame
- Concat DataFrames in Pandas
- Append Rows to a Pandas DataFrame
- Compare Two DataFrames for Equality in Pandas
- Get Column Names as List in Pandas DataFrame
- Select One or More Columns in Pandas
- Pandas – Rename Column Names
- Pandas – Drop one or more Columns from a Dataframe
- Pandas – Iterate over Rows of a Dataframe
- How to Reset Index of a Pandas DataFrame?
- Read CSV files using Pandas – With Examples
- Apply a Function to a Pandas DataFrame
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.