How to reset index in Pandas

You can reset the index in Pandas with the following code. I highly recommend you This book to learn Python.

Step 1: Install Pandas Library

Install the Pandas library using this code, if it is not installed.

pip install pandas

Example

In this example, I will reset the index of Pandas DataFrame using df.reset_index().

# Import the Pandas library as pd
import pandas as pd

# Initialize a dictionary
dict = {'Students':['Ali', 'Amini', 'Alexandar', 'John'],
        'Scores':[94, 93, 83, 95]}

# Create DataFrame from dictionary
df = pd.DataFrame(dict, index=['A', 'B','C','D'])

# Display Original DataFrame
print(df)

# Reset index without new column
df = df.reset_index(drop=True)

# Display the DataFrame
print(df)

Output:

    Students  Scores
A        Ali      94
B      Amini      93
C  Alexandar      83
D       John      95
    Students  Scores
0        Ali      94
1      Amini      93
2  Alexandar      83
3       John      95

Free Learning Resources

Leave a Comment

Your email address will not be published.