Learn about Reset Index Pandas

Introduction

In this article, we will learn about reset index pandas on pandas DataFrame with several examples. We have previously learned that Pandas is a Python library used for working with data sets. It offers tools for data exploration, cleaning, analysis, and manipulation.

To reset the index on the pandas DataFrame, use the DataFrame.reset index() method. This returns a DataFrame and accepts the arguments level, drop, inplace, col level, and col fill. Use of inplace=True causes it to return Nothing. To set a column as the index, use pandas.DataFrame.set index().

Below are some quick examples of reset index on DataFrame.

# Quick examples of reset index
# Reset index
df2=df.reset_index()

# Reset index without assigning to column
df.reset_index(inplace=True, drop=True)

# set index column name
df.index.name = 'custom_index'

# Reset index starting from 1
df.index = np.arange(1, len(df) + 1)

pandas.DataFrame.reset_index() Syntax

Following is the syntax of the pandas.DataFrame.reset_index().

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

  • level – Takes int, str, tuple, or list, default None
  • drop – bool, default False. When set to True, it ignores setting the old index as a column.
  • inplace – bool, default False. When set to True, it updates the existing DataFrame object.
  • col_level – If the columns have multiple levels, determine which level the labels are inserted into.
  • col_fill – If the columns have multiple levels, determine how the other levels are named.

Let’s create a pandas DataFrame with examples

import pandas as pd

# Create DataFrame from dict
df = pd.DataFrame({'Courses':['Spark','PySpark','Java','PHP'],
          'Fee':[20000,20000,15000,10000],
          'Duration':['35days','35days','40days','30days']})

df=df.drop([2])
print(df)

Yields below output

  Courses    Fee Duration
0    Spark  20000   35days
1  PySpark  20000   35days
3      PHP  10000   30days

pandas Reset Index on DataFrame Example

Now let's reset the DataFrame index using the pandas.DataFrame.reset index() syntax. The old index is added to the DataFrame as a column when the index is reset, and a new sequential index is used instead.

# reset index on DataFrame
df2=df.reset_index()
print(df2)

Yields below output.

  index  Courses    Fee Duration
0      0    Spark  20000   35days
1      1  PySpark  20000   35days
2      3      PHP  10000   30days

If you don’t want to add the existing index as a column, use drop=True param and also use inplace=True to update the existing DataFrame.

# drop index as column
df.reset_index(inplace=True, drop=True)
print(df)

Yields below output

  Courses    Fee Duration
0    Spark  20000   35days
1  PySpark  20000   35days
2      PHP  10000   30days

Reset index name on DataFrame

Let's now examine how to rename an index in a pandas DataFrame. DataFrame is created by default with the index name set to "index."

# Set index name
df.index.name = 'custom_index'
print(df)

Yields below output.

              Courses    Fee Duration
custom_index                        
0               Spark  20000   35days
1             PySpark  20000   35days
2                 PHP  10000   30days

Complete Example of Reset Index on DataFrame

import pandas as pd
import numpy as np

# Create DataFrame from dict
df = pd.DataFrame({'Courses':['Spark','PySpark','Java','PHP'],
          'Fee':[20000,20000,15000,10000],
          'Duration':['35days','35days','40days','30days']})

df=df.drop([2])
print(df)

# Reset index
df2=df.reset_index()
print(df2)

# Reset index without assigning to column
df.reset_index(inplace=True, drop=True)
print(df)

# set index column name
df.index.name = 'custom_index'
print(df)

# Reset index starting from 1
df.index = np.arange(1, len(df) + 1)
print(df)

write your code here: Coding Playground

Conclusion

In this article, you have learned how to reset the index on DataFrame by using different prams on the reset_index() method.