In this tutorial, we will look at how to rename the column resulting from the reset_index()
function in pandas with the help of some examples.
The pandas dataframe reset_index()
function is used to reset the index of a dataframe and by default, it adds the previous index as a separate column in the dataframe (pass drop=True
if you do not want to retain the previous index as a separate column).
Methods to rename the column resulting from reset_index()
If the index axis itself doesn’t have a name, the new column is added with the name “index”. You can, however, change the name of this column in the following ways –
- For pandas version
1.5.0
and above, when calling thereset_index()
function, pass the name you’d like the column (with the previous index) to have with the help of thenames
parameter. Pass a list of names if the dataframe has a multi-index. - Before using the
reset_index()
function, give the index axis the name you’d like the resulting column to have using therename_axis()
function and then apply thereset_index()
function. - After applying the
reset_index()
function, apply the pandas dataframerename()
function on the resulting dataframe to change one or more column names as per your requirements.
Let’s now look at both these methods with the help of some examples.
First, we will create a dataframe that we’ll use throughout this tutorial.
import pandas as pd # employee data data = { "Department": ["Sales", "Sales", "Accounting", "HR"], "Age": [26, 28, 27, 32], "Salary": [70000, 80000, 72000, 66000] } # create pandas dataframe df = pd.DataFrame(data, index=["Jim", "Dwight", "Angela", "Tobi"]) # display the dataframe df
Output:

Here, we created a dataframe with some information about some employees in an office. You can see that the index in this particular dataframe is the name of the respective employee.
Now, if you were to apply the reset_index()
function with default parameters. You’d get the previous index (we say previous index because the reset_index() function “resets” the index to the default integer index starting from 0) as a separate column.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 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.reset_index()
Output:

Notice, that the column with the previous index has the name “index”. This is because the original index in the dataframe didn’t have a name for itself.
Now, if you’d like the new column to have a different name, you can use one of the following three methods –
- In the call to the
reset_index()
function, specify the name you’d like the column with the original index data to have using thenames
parameter. Note that this method works only forpandas
version1.5.0
and above (as thenames
parameter was introduced to this function in the version1.5.0
). - Before applying the
reset_index()
function, give the index axis a name (the name you’d like the column to have), and then apply thereset_index()
method. - After applying the
reset_index()
function, use the pandas dataframerename()
function to change the column names to your liking.
Let’s look at the three methods with examples.
Using the names
parameter in the reset_index()
function
The idea is to pass the name of the column with the original index data as an argument to the names
parameter in the reset_index()
function.
Note that the names
parameter was introduced in the pandas version 1.5.0
, so check your pandas version if this method is not working for you.
Let’s give the resulting column the name “Employee”.
# reset index with custom name for the new original index column df.reset_index(names="Employee")
Output:

You can see that now the column with the original index data has the name “Employee”
Note that if your dataframe has a multi-index, you can pass a list to the names
parameter.
Rename the axis before applying reset_index()
Here, we give the index axis a name (the name that we want the resulting column to have) and then apply the reset_index()
function. You can rename an axis using the pandas dataframe rename_axis()
function, which renames the index axis by default.
Let’s first display our original dataframe.
df
Output:

Let’s now use this method to rename the axis first and then reset the index.
# reset index with custom name for the new original index column df.rename_axis("Employee").reset_index()
Output:

We get the same result as above. The original index data with the column name “Employee”.
Rename the column after applying the reset_index()
function
In this method, we first apply the reset_index()
function and then change the column name using the pandas dataframe rename()
function.
First, let’s apply the reset_index()
function and save the resulting dataframe.
# reset the index of the dataframe df = df.reset_index() # display the dataframe df
Output:

Now, let’s rename the “index” column with the column name “Employee”
# rename column name df = df.rename(columns={"index": "Employee"}) # display the dataframe df
Output:

We now get the same result.
You might also be interested in –
- Pandas – Rename Columns After Merge
- Pandas – Rename Columns in Dataframe after Groupby
- Pandas – Rename Column Names
- Reset Index in Pandas – With Examples
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.