How to Reset an Index in Pandas DataFrame

In this short guide, you’ll see how to reset an index in Pandas DataFrame.

In general, you can reset an index in Pandas DataFrame using this syntax:

df.reset_index(drop=True)

Let’s now review the steps to reset your index using a simple example.

Steps to Reset an Index in Pandas DataFrame

Step 1: Gather your data

For illustration purposes, let’s gather the following data about various products:

Product Price
Tablet 250
Printer 150
Laptop 1200
Monitor 300
Computer 1500

Step 2: Create a DataFrame

Next, create a DataFrame to capture the above data in Python:

import pandas as pd

data = {'Product': ['Tablet', 'Printer', 'Laptop', 'Monitor', 'Computer'], 
        'Price': [250, 150, 1200, 300, 1500]
        }

df = pd.DataFrame(data, columns= ['Product', 'Price'])

print(df)

When you run the code in Python, you’ll get the following DataFrame:

    Product  Price
0    Tablet    250
1   Printer    150
2    Laptop   1200
3   Monitor    300
4  Computer   1500

Notice that the index values (highlighted in yellow) are sequential from 0 to 4, such that the:

  • First row (Tablet for a price of 250) has an index of 0
  • Second row (Printer for a price of 150) has an index of 1
  • Third row (Laptop for a price of 1200) has an index of 2
  • Fourth row (Monitor for a price of 300) has an index of 3
  • Fifth row (Computer for a price of 1500) has an index of 4

Step 3: Drop Rows from the DataFrame

Before you reset the index in the DataFrame, let’s create a scenario where the index will no longer be sequential.

One way to do that is by dropping some of the rows from the DataFrame. For example, let’s drop the first row (index of 0), as well as the fourth row (index of 3):

df = df.drop([0, 3])

So the full code would look like this:

import pandas as pd

data = {'Product': ['Tablet', 'Printer', 'Laptop', 'Monitor', 'Computer'], 
        'Price': [250, 150, 1200, 300, 1500]
        }

df = pd.DataFrame(data, columns= ['Product', 'Price'])
df = df.drop([0, 3])

print(df)

You’ll now notice that the index is no longer sequential:

    Product  Price
1   Printer    150
2    Laptop   1200
4  Computer   1500

Step 4: Reset the Index in Pandas DataFrame

To reset the index in the DataFrame you’ll need to apply the following syntax:

df.reset_index(drop=True)

Putting everything together:

import pandas as pd

data = {'Product': ['Tablet', 'Printer', 'Laptop', 'Monitor', 'Computer'], 
        'Price': [250, 150, 1200, 300, 1500]
        }

df = pd.DataFrame(data, columns= ['Product', 'Price'])
df = df.drop([0, 3])
df = df.reset_index(drop=True)

print(df)

You’ll now get a sequential index that starts from 0:

    Product  Price
0   Printer    150
1    Laptop   1200
2  Computer   1500