rename column after reset index in pandas

Pandas – Rename Column after Reset Index

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 –

  1. For pandas version 1.5.0 and above, when calling the reset_index() function, pass the name you’d like the column (with the previous index) to have with the help of the names parameter. Pass a list of names if the dataframe has a multi-index.
  2. Before using the reset_index() function, give the index axis the name you’d like the resulting column to have using the rename_axis() function and then apply the reset_index() function.
  3. After applying the reset_index() function, apply the pandas dataframe rename() 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:

the original dataframe with employee data

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.

📚 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.reset_index()

Output:

dataframe after reset_index() on original dataframe

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

  1. In the call to the reset_index() function, specify the name you’d like the column with the original index data to have using the names parameter. Note that this method works only for pandas version 1.5.0 and above (as the names parameter was introduced to this function in the version 1.5.0).
  2. Before applying the reset_index() function, give the index axis a name (the name you’d like the column to have), and then apply the reset_index() method.
  3. After applying the reset_index() function, use the pandas dataframe rename() 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:

the column resulting from reset_index() has the name "Employee"

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:

the original dataframe with employee data

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:

the column resulting from reset_index() has the name "Employee"

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:

dataframe after reset_index() on original dataframe

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:

the column resulting from reset_index() has the name "Employee"

We now get the same result.

You might also be interested in –


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