reset index of a dataframe pandas

Reset Index in Pandas – With Examples

Pandas dataframes are quite powerful for manipulating data. Often you may require to re-index your dataframe to the default index. In this tutorial, we’ll cover how to reset the index of a pandas dataframe with some examples.

As you apply common operations such as filtering, removing NaNs, etc, it may happen that you end up with possibly a smaller dataframe whose indices are not continuous. For example:

import pandas as pd

# create a sample dataframe
data = {
    'A': ['a1', 'a2', 'a3', 'a4'],
    'B': [1, 2, 3, 4],
    'C': ['c1', 'c2', 'c3', 'c4']
}
df = pd.DataFrame(data)

# filter the dataframe for only odd values of B
df_filtered_odd = df[df['B']%2!=0]

The dataframes df and df_filtered_odd look as follows:

Dataframes with continuous and non-continuous indices

Having a dataframe with non-continuous indices may not be an issue by itself but performing operations on such dataframes along with dataframes with continuous or a different index scheme could give some unintended results. For example:

df_filtered_even = df[df['B']%2==0]
print(df_filtered_odd['B']+df_filtered_even['B'])

Output:

0   NaN
1   NaN
2   NaN
3   NaN
Name: B, dtype: float64

In the above example, we create the dataframe df_filtered_even by filtering df for even values of B. Both the dataframes df_filtered_odd and df_filtered_even are of length two but if we perform an addition operation with both of them on the column B we get a series of four NaNs. This happened because row operations are performed at the index level and since neither of the two dataframe had a matching index we got four NaNs.

You can use the pandas dataframe reset_index() function to set the index of a dataframe to its default (i.e. continuous numbers from zero). The following is its syntax:

df.reset_index()

The above function returns a copy of your dataframe with its old index as a new column and having a continuous integer index from 0.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

  • Pass drop=True to the above function if you don’t want the old index as a new column in your dataframe.
  • Pass inplace=True if you want to modify the dataframe in-place.

Example 1: reset_index() with default parameters

import pandas as pd

data = {
    'Name': ['Sam', 'Tim', 'Rahul', 'Emma', 'Kyle'],
    'Age': [14, 21, 16, 18, 23],
    'Country': ['UK', 'India', 'USA', 'Germany', 'France'],
    'Language': ['English', 'Hindi', 'English', 'German', 'French']
}

df = pd.DataFrame(data, index=[1,3,5,7,9])

print("Before reset index:\n")
print(df)

# reset the index with default parameters
df = df.reset_index()

print("\nAfter reset index:\n")
print(df)

Output:

Before reset index:

    Name  Age  Country Language
1    Sam   14       UK  English
3    Tim   21    India    Hindi
5  Rahul   16      USA  English
7   Emma   18  Germany   German
9   Kyle   23   France   French

After reset index:

   index   Name  Age  Country Language
0      1    Sam   14       UK  English
1      3    Tim   21    India    Hindi
2      5  Rahul   16      USA  English
3      7   Emma   18  Germany   German
4      9   Kyle   23   France   French

In the above example, we reset the index of the dataframe df using reset_index() with default parameters. We can see that a new column by the name index has been created storing the values of the old index and the dataframe’s index is reset to continuous integers from 0.

Example 2: With drop=True parameter

import pandas as pd

data = {
    'Name': ['Sam', 'Tim', 'Rahul', 'Emma', 'Kyle'],
    'Age': [14, 21, 16, 18, 23],
    'Country': ['UK', 'India', 'USA', 'Germany', 'France'],
    'Language': ['English', 'Hindi', 'English', 'German', 'French']
}

df = pd.DataFrame(data, index=[1,3,5,7,9])

print("Before reset index:\n")
print(df)

# reset the index with drop=True
df = df.reset_index(drop=True)

print("\nAfter reset index:\n")
print(df)

Output:

Before reset index:

    Name  Age  Country Language
1    Sam   14       UK  English
3    Tim   21    India    Hindi
5  Rahul   16      USA  English
7   Emma   18  Germany   German
9   Kyle   23   France   French

After reset index:

    Name  Age  Country Language
0    Sam   14       UK  English
1    Tim   21    India    Hindi
2  Rahul   16      USA  English
3   Emma   18  Germany   German
4   Kyle   23   France   French

You can see that using the drop=True gave a dataframe without the old index as an additional column.

The reset_index() function works with MultiIndex as well. For more, refer to its documentation.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top