Skip to content

Pandas Reset Index: How to Reset a Pandas Index

Pandas Reset Drop Index How to Reset a Pandas Index Cover Image

Welcome to this tutorial on resetting a Pandas DataFrame index. In this post, you’ll learn how to use Pandas’ powerful .reset_index() method to refine your DataFrame’s index, produce clean data structures for your analytics and modeling tasks and unlock deeper insights from your data.

Resetting and working with a Pandas index is something I encounter every day working with Pandas. This guide covers my favorite ways to reset an index – all of which are tried and tested! We’ll cover important topics like:

  • How .reset_index() works and when to use it
  • How to reset an index and drop the original column
  • How to drop an index on a multi-index DataFrame
  • How to reset an index in-place

Whether you’re new to Pandas or an advanced user seeking to enhance your indexing skills, this tutorial will provide you with the knowledge and expertise you need to handle complex indexing operations with ease. So, let’s dive in and explore how to reset your DataFrame’s index using Pandas’ reset_index() method.

The Quick Answer: The Different Ways to Reset an Index

# The Different Ways to Reset an Index
import pandas as pd
df = pd.DataFrame()

# Reset an index and keep it
df = df.reset_index()  

# Reset index and drop it
df = df.reset_index(drop=True)   

# Reset index in place
df.reset_index(inplace=True)

# Reset a Level of a Multi-Index DataFrame
df = df.reset_index(level=1)

Loading a Sample Dataframe

To follow along with this tutorial, I have provided a sample Pandas DataFrame below. You can, of course, use your own dataset, though your results will vary. I have kept the dataset deliberately simple so you can more easily follow along.

# Load a sample pandas dataframe
import pandas as pd
df = pd.DataFrame(
    {'Name': ['Jane', 'Melissa', 'John', 'Matt'],
     'Age': [23, 45, 35, 64],
     'Birth City': ['Toronto', 'Atlanta', 'Toronto', 'Atlanta'],
     'Gender': ['F', 'F', 'M', 'M']}).set_index('Name')

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

In the code block above, we loaded a Python dictionary into a DataFrame. We then applied the .set_index() method to the DataFrame to have a more meaningful index.

How to Reset a DataFrame Index in Pandas

In this section, we’ll take a closer look at how the .reset_index() method works and when you should use it in your data analysis tasks.

By understanding the nuances of the .reset_index() method, you’ll be able to transform your data into clean, structured, and more intuitive data frames that are better suited to your needs.

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

# Understanding the Pandas reset_index() Method
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:

ParameterDescriptionDefault Value
level=Allows us to specify which levels of a multi-index DataFrame. By default, it will reset all levelsNone
drop=Allows us to specify that the original index should be dropped (rather than being inserted as a new DataFrame column)False
inplace=Whether to modify the DataFrame rather than create a new one.False
col_level=Allows us to specify which level the labels of columns should be inserted to if they have multiple levels.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.
The parameters of the Pandas .reset_index() method

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

# How to reset a Pandas dataframe index
import pandas as pd
df = pd.DataFrame({'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 take a look in more detail about how we used the .reset_index() method to reset the index and include it in the dataset:

  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 look at how we can reset an index in Pandas and drop the original column’s values.

How to Drop a DataFrame Index in Pandas

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=, which 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
import pandas as pd
df = pd.DataFrame({'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(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

In the previous sections of this tutorial, we reset an index and always re-assigned the DataFrame to itself. We did this because the method returns a DataFrame in itself.

However, we may not always want to have to re-assign the DataFrame. 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.

One advantage of using inplace=True is that it can save memory by modifying the original object instead of creating a new one. This can be important when working with very large datasets where memory usage is a concern.

One of the drawbacks of this approach is that the code becomes less readable. Because most Pandas operations can also happen in-place, it’s important to stay consistent. Personally, I recommend sticking to re-assigning the DataFrame to itself, unless I’m working with a particularly large dataset.

Let’s see how we can drop a Pandas DataFrame’s index in place:

# Reset a Dataframe Index inlace
import pandas as pd
df = pd.DataFrame({'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)

df.reset_index(inplace=True)

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

Running this code 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

We can see from the printout above that the DataFrame’s index was dropped in-place. This means that the result looks the same as what we saw before. 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

If you work with multi-level index Pandas DataFrames, it might feel tricky to manipulate and manage your data’s indexing structure. Thankfully, the .reset_index() method can help simplify your multi-index DataFrame indexing by allowing you to collapse one or more index levels into a single column, or even completely remove the index.

In this section, you’ll learn how to reset an index on a multi-index Pandas DataFrame, so you can restructure the DataFrame’s levels to suit your analysis needs or export the data to other applications. Let’s start by loading a new sample DataFrame:

# Creating a Multi-Index Pandas DataFrames
import pandas as pd
df = pd.DataFrame({'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)

# Returns:
#                 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 allow 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

We can see from the code blocks above that both of the index columns were reset and inserted into the DataFrame as columns. In the original multi-level index, repeated values in the columns weren’t shown. However, in the resulting DataFrame, these values are copied down.

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

How to Reset Only One Level of a Multi-Level Pandas Index

When it comes to resetting a multi-index Pandas DataFrame, you might not always want to reset every level of its indices. Luckily, Pandas offers a powerful feature that allows you to selectively reset certain levels of your DataFrame’s multi-index, known as the level= parameter.

By using the level= parameter, you can target specific index levels and reset only those levels while leaving other levels unchanged. This can be particularly useful when you need to preserve certain hierarchical groupings within your data but want to reorganize other levels of the indexing structure.

# Reset only one level of a multi-index Pandas DataFrame
import pandas as pd
df = pd.DataFrame({'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('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

By running the .reset_index(level = 1) method on this DataFrame, we selectively reset the second level of the multi-index, i.e. ‘Name,’ converting it from a multi-level index to a single index, while the ‘Gender’ level remains unchanged. 

Conclusion

Congratulations! You’ve learned how to reset Pandas DataFrame indices like a pro! In this tutorial, we started with the basics and covered some of the most crucial and practical applications of the .reset_index() method in Pandas.

Whether you’re an experienced data analyst or just getting started with Python and Pandas, mastering the .reset_index() method is essential when working with complex data structures.

So, what’s next? Try experimenting with .reset_index() on your own data frames and explore the impact of different parameter values, such as the level parameter. Additionally, you can explore other Pandas functions that complement .reset_index(), like .set_index(), to further refine and manipulate your data structures.

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

Additional Resources

To learn more about related topics, check out the resources below:

Nik Piepenbreier

Nik is the author of datagy.io and has over a decade of experience working with data analytics, data science, and Python. He specializes in teaching developers how to use Python for data science using hands-on tutorials.View Author posts

2 thoughts on “Pandas Reset Index: How to Reset a Pandas Index”

  1. Pingback: Pandas Sum: Add Dataframe Columns and Rows • datagy

  2. Pingback: Pandas: How to Drop a Dataframe Index Column • datagy

Leave a Reply

Your email address will not be published. Required fields are marked *