How to Reset an Index in Pandas DataFrame

In this short post, I’ll show you how to reset an index in pandas DataFrame. I’ll review a full example to demonstrate this concept in Python.

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 an example.

Steps to Reset an Index in Pandas DataFrame

Step 1: Gather your data

For illustration purposes, I gathered the following data about various products:

ProductPrice
Tablet250
iPhone800
Laptop1200
Monitor300
Computer1500

Step 2: Create a DataFrame

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

import pandas as pd

myProducts = {'Product': ['Tablet', 'iPhone', 'Laptop', 'Monitor', 'Computer'], 
              'Price': [250, 800, 1200, 300, 1500], 
              }

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

print(df)

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

index of pandas dataframe

Notice that the index numbers, which are assigned to the rows in the DataFrame, are sequential from 0 to 4, such that the:

  • First row (Tablet for a price of 250) has an index of 0
  • Second row (iPhone for a price of 800) 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 your 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

myProducts = {'Product': ['Tablet', 'iPhone', 'Laptop', 'Monitor', 'Computer'], 
              'Price': [250, 800, 1200, 300, 1500], 
              }

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

print(df)

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

Reset an Index in Pandas DataFrame

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

myProducts = {'Product': ['Tablet', 'iPhone', 'Laptop', 'Monitor', 'Computer'], 
              'Price': [250, 800, 1200, 300, 1500], 
              }

df = pd.DataFrame(myProducts, 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:

How to Reset an Index in Pandas DataFrame