pandas
  1. pandas-reset-index

Reset Index - ( Pandas Indexing )

Heading h2

Syntax

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

Example

import pandas as pd

# create a dataframe
df = pd.DataFrame({'Name':['John', 'Emily', 'Charlie'], 'Age':[25, 30, 35]})

# reset index
df = df.reset_index()

print(df)

Output

   index     Name  Age
0      0     John   25
1      1    Emily   30
2      2  Charlie   35

Explanation

The reset_index() method is a Pandas function used to reset the index of a dataframe to a default sequential numerical index. It returns a new dataframe object with the index reset.

In the above example, a simple dataframe is created with columns 'Name' and 'Age'. The reset_index() method is used to reset the index to a default sequential numerical index starting from 0.

Use

The reset_index() method is used to reset the index of a Pandas dataframe to a default sequential numerical index. This can be useful when dealing with dataframes that have non-sequential or non-unique indexes.

Important Points

  • The reset_index() method resets the index of a Pandas dataframe to a default sequential numerical index
  • It returns a new dataframe object with the index reset
  • The original dataframe is not modified by default. However, it can be modified in place using the inplace=True argument.

Summary

In conclusion, the reset_index() method is a useful function in Pandas for resetting the index of a dataframe to a default sequential numerical index. It returns a new dataframe object with the index reset. The method can be useful when dealing with dataframes that have non-sequential or non-unique indexes. Optionally, the inplace=True argument can be used to modify the original dataframe object.

Published on: