Pandas: DataFrame.reset_index() not working [Solved]

avatar
Borislav Hadzhiev

Last updated: Apr 12, 2024
3 min

banner

# Table of Contents

  1. Pandas: DataFrame.reset_index() not working [Solved]
  2. Set the drop argument to False to remove the additional column
  3. Resetting the index in place by mutating the original DataFrame

# Pandas: DataFrame.reset_index() not working [Solved]

The DataFrame.reset_index() might not work if you forget to assign the resulting DataFrame to a variable.

By default, the method resets the DataFrame index and returns a new DataFrame containing the results. It doesn't reset the index in place.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 1, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['A', 'B', 'C', 'D']) print(df) df = df.reset_index() print('-' * 50) print(df)
The code for this article is available on GitHub

Running the code sample produces the following output.

shell
name experience salary A Alice 1 175.1 B Bobby 3 180.2 C Carl 1 190.3 D Dan 7 205.4 -------------------------------------------------- index name experience salary 0 A Alice 1 175.1 1 B Bobby 3 180.2 2 C Carl 1 190.3 3 D Dan 7 205.4

using dataframe reset index correctly

Notice that we assigned the result of calling the DataFrame.reset_index() method to a variable.

main.py
df = df.reset_index()
The reset_index method resets the index of the DataFrame and returns the DataFrame with the new index (starting at 0).

The method doesn't reset the DataFrame's index in place, so make sure to store the result in a variable.

# Set the drop argument to False to remove the additional column

By default, when you call the reset_index method, the current index of the DataFrame gets added as a column.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 1, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['A', 'B', 'C', 'D']) df = df.reset_index() # index name experience salary # 0 A Alice 1 175.1 # 1 B Bobby 3 180.2 # 2 C Carl 1 190.3 # 3 D Dan 7 205.4 print(df)

previous index values get added as column

The code for this article is available on GitHub

Notice that the DataFrame now has an index column with its previous index values.

You can set the drop argument to True when calling reset_index if you want to not insert the index column.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 1, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['A', 'B', 'C', 'D']) df = df.reset_index(drop=True) # name experience salary # 0 Alice 1 175.1 # 1 Bobby 3 180.2 # 2 Carl 1 190.3 # 3 Dan 7 205.4 print(df)

setting drop argument to true when calling reset index

The code for this article is available on GitHub

When the drop argument is set to True, the additional index column doesn't get inserted.

# Resetting the index in place by mutating the original DataFrame

The previous calls to the reset_index() method return a new DataFrame with the index reset.

However, you can set the inplace argument if you'd rather reset the index of the original DataFrame and return None.

main.py
import pandas as pd df = pd.DataFrame({ 'name': ['Alice', 'Bobby', 'Carl', 'Dan'], 'experience': [1, 3, 1, 7], 'salary': [175.1, 180.2, 190.3, 205.4], }, index=['A', 'B', 'C', 'D']) df.reset_index(drop=True, inplace=True) # name experience salary # 0 Alice 1 175.1 # 1 Bobby 3 180.2 # 2 Carl 1 190.3 # 3 Dan 7 205.4 print(df)

resetting the dataframe index in place

The code for this article is available on GitHub

Notice that we didn't assign the result of calling reset_index to a variable.

When the inplace argument is set to True, the reset_index method returns None because it resets the DataFrame index in place via a mutation.

There is a convention in Python that methods that mutate the original object return None.

When using this approach, make sure to not assign the result of calling reset_index to a variable because the variable would store None.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.