Pandas Reset Index: How to Reset a Pandas Index

Pandas Reset Index Cover Image

In this post, you’ll learn how to use Pandas’ reset_index() to reset a Pandas Index. You’ll learn how the reset_index() method works, how to reset an index and drop the original column, how to drop an index on a multi-index dataframe, and how to reset an index in-place.

The Quick Answer: Use Pandas .reset_index() method

How to easily reset a Pandas dataframe index with reset_index

Loading a Sample Dataframe

Feel free to follow along with your own data while reading this tutorial. If, however, you don’t have a dataframe or want to follow along step-by-step, feel free to load the same dataframe provided below by copying and pasting the code into your favourite code editor!

Let’s get started:

# Load a sample pandas dataframe
import pandas as pd

df = pd.DataFrame.from_dict(
    {'Name': ['Jane', 'Melissa', 'John', 'Matt'],
     'Age': [23, 45, 35, 64],
     'Birth City': ['Toronto', 'Atlanta', 'Toronto', 'Atlanta'],
     'Gender': ['F', 'F', 'M', 'M']}).set_index('Name')

print(df)

This returns the following dataframe:

         Age Birth City Gender
Name                          
Jane      23    Toronto      F
Melissa   45    Atlanta      F
John      35    Toronto      M
Matt      64    Atlanta      M

Now that you have a dataframe ready to go, let’s get started in learning how to reset an index in a Pandas dataframe.

How to Reset an Index in Pandas

Pandas comes built in with a handy dataframe method, the .reset_index() method, that lets you, well, reset a Pandas dataframe’s index.

Let’s take a look at the method’s syntax:

DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')

Let’s take a look at what the different parameters do:

  • level= allows us to specify which levels of a multi-index dataframe. By default, it will reset all levels.
  • drop= allows us to specify that the original index should be dropped (rather than being inserted as a new dataframe column)
  • col_level= allows us to specify which level the labels of columns should be inserted to, if they have multiple levels. The default level is set to 0.
  • col_fill= allows you to specify, if the dataframe has multiple levels, what the other levels should be filled with. If None is passed, then the index name is repeated.

Now that we know how the method works in theory, let’s practise how we can actually reset an index.

# How to reset a Pandas dataframe index
import pandas as pd

df = pd.DataFrame.from_dict(
    {'Name': ['Jane', 'Melissa', 'John', 'Matt'],
     'Age': [23, 45, 35, 64],
     'Birth City': ['Toronto', 'Atlanta', 'Toronto', 'Atlanta'],
     'Gender': ['F', 'F', 'M', 'M']}).set_index('Name')

print('The original dataframe:')
print(df)

reset_index_df = df.reset_index()

print('\nThe new dataframe:')
print(reset_index_df)

This returns the following dataframes:

The original dataframe:
         Age Birth City Gender
Name                          
Jane      23    Toronto      F
Melissa   45    Atlanta      F
John      35    Toronto      M
Matt      64    Atlanta      M

The new dataframe:
      Name  Age Birth City Gender
0     Jane   23    Toronto      F
1  Melissa   45    Atlanta      F
2     John   35    Toronto      M
3     Matt   64    Atlanta      M

Let’s see what has happened here:

  1. We created a dataframe and printed out the original version. In that, we can see that the dataframe has a named index, Name
  2. We then run apply the .reset_index() method to the dataframe, without passing any arguments.
  3. This returns a dataframe with the original index inserted into the dataframe as a column.

Now let’s take a look at how we can reset an index in Pandas and drop the original.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

How to Reset an Index in Pandas and Drop the Original

By default, Pandas will convert the original index into a dataframe column. This may not always be what you want to happen. Thankfully, Pandas provides a helpful parameter, drop=, that allows us to drop the original index.

By default, the parameter will be set to False, meaning the original index will not be dropped. If we set the parameter to drop=True, then the original index will not be inserted into the dataframe as a column

Let’s see how this look:

# Reset a dataframe index and drop the original index
print('The original dataframe:')
print(df)

reset_index_df = df.reset_index(drop=True)

print('\nThe new dataframe:')
print(reset_index_df)

Let’s see what these dataframes look like:

The original dataframe:
         Age Birth City Gender
Name                          
Jane      23    Toronto      F
Melissa   45    Atlanta      F
John      35    Toronto      M
Matt      64    Atlanta      M

The new dataframe:
   Age Birth City Gender
0   23    Toronto      F
1   45    Atlanta      F
2   35    Toronto      M
3   64    Atlanta      M

We can see above that when we reset the index and set drop=True, that the original index was not inserted into the dataframe as a column.

In the next section, you’ll learn how to reset a Pandas index in-place, meaning you don’t need to re-assign it.

How to Reset a Pandas Index In-Place

You may not always want to have to re-assign a dataframe when resetting its index. Because of this, Pandas allows you to reset a Pandas index in-place, using the inplace= parameter. By default, this parameter will be set to False.

However, you can set the parameter to True, in order to not have to reassign the dataframe.

Let’s see how this can work:

# Reset a Dataframe Index inlace
print('The original dataframe:')
print(df)

df.reset_index(inplace=True)

print('\nThe new dataframe:')
print(df)

This returns the following:

The original dataframe:
         Age Birth City Gender
Name                          
Jane      23    Toronto      F
Melissa   45    Atlanta      F
John      35    Toronto      M
Matt      64    Atlanta      M

The new dataframe:
      Name  Age Birth City Gender
0     Jane   23    Toronto      F
1  Melissa   45    Atlanta      F
2     John   35    Toronto      M
3     Matt   64    Atlanta      M

In the next section, you’ll learn how to reset a multi-level Pandas index – let’s get started!

How to Reset a Multi-Level Pandas Index

So far, we have explored how to reset the index of a single-level dataframe. In this section, you’ll learn how to reset multi-level indices! Let’s get started by loading a new dataframe:

import pandas as pd

df = pd.DataFrame.from_dict(
    {'Name': ['Jane', 'Melissa', 'John', 'Matt'],
     'Age': [23, 45, 35, 64],
     'Birth City': ['Toronto', 'Atlanta', 'Toronto', 'Atlanta'],
     'Gender': ['F', 'F', 'M', 'M']}).set_index(['Gender', 'Name'])

print(df)

This returns the following dataframe:

                Age Birth City
Gender Name                   
F      Jane      23    Toronto
       Melissa   45    Atlanta
M      John      35    Toronto
       Matt      64    Atlanta

We can see here, that we have two indices. Resetting them is easy – it works just as though there were only one. This is because the default arguments that are passed on allows us to reset all levels (using level=None) and fill in missing values using col_fill=''.

Because of these default parameters, when we pass in our .reset_index() method, we receive the results you’ve come to expect!

print('The original dataframe:')
print(df)

reset_index_dataframe = df.reset_index()

print('\nThe new dataframe:')
print(reset_index_dataframe)

This returns the following dataframe:

The original dataframe:
                Age Birth City
Gender Name                   
F      Jane      23    Toronto
       Melissa   45    Atlanta
M      John      35    Toronto
       Matt      64    Atlanta

The new dataframe:
  Gender     Name  Age Birth City
0      F     Jane   23    Toronto
1      F  Melissa   45    Atlanta
2      M     John   35    Toronto
3      M     Matt   64    Atlanta

In the next section, you’ll learn how to reset only one level of a multi-index dataframe.

Reset Only One Level of a Multi-Level Pandas Index

When working with a multi-index Pandas dataframe, you may not always want to reset every level of its indices. Let’s see how we can use the level= parameter to only reset certain levels of the multi-index.

print('The original dataframe:')
print(df)

reset_index_dataframe = df.reset_index(level=1)

print('\nThe new dataframe:')
print(reset_index_dataframe)

This returns the following dataframe:

The original dataframe:
                Age Birth City
Gender Name                   
F      Jane      23    Toronto
       Melissa   45    Atlanta
M      John      35    Toronto
       Matt      64    Atlanta

The new dataframe:
           Name  Age Birth City
Gender                         
F          Jane   23    Toronto
F       Melissa   45    Atlanta
M          John   35    Toronto
M          Matt   64    Atlanta

We can see here that level 1, meaning the second index, has been reset, while the first level (level 0) has been kept!

Conclusion

In this post, you learned how to reset a Pandas dataframe index, including different ways in which you can pass in parameters to manipulate how this is handled. You learned how to keep the original index as a column, how to drop the original index, how to reset an index in place, and how to work with multi-index dataframes.

To learn more about the Pandas reset_index() method, check out the official documentation here.