Reset Index of Pandas DataFrame

  • Post category:Pandas

In this Pandas tutorial, you will learn how to reset index of Pandas DataFrame in Python. You can easily reset the index of DataFrame with the following code.

# import Pandas library
import pandas as pd

# create a dataframe
var = pd.DataFrame({'A': [10, 5, 40, 20], 'B': [50, 40, 30, 10], 'C': [90, 30, 80, 40]})

# sort the dataframe by column A
var.sort_values(by=['A'], inplace=True)

# display the dataframe
print(var)

# reset index of the dataframe
var.reset_index(drop=True, inplace=True)

# display the dataframe after reset index
print(var)

Output:

    A   B   C
1   5  40  30
0  10  50  90
3  20  10  40
2  40  30  80
    A   B   C
0   5  40  30
1  10  50  90
2  20  10  40
3  40  30  80

Free resources to learn advanced skills: AiHints and CodeAllow