0
0
Pandasdata~5 mins

Resetting index in Pandas

Choose your learning style9 modes available
Introduction
Resetting the index helps you turn the current index back into a normal column and get a new default index. This makes the data easier to work with or prepare for saving.
When you have filtered or sorted data and want to clean up the index numbers.
When you want to convert the index into a regular column for analysis or export.
When you have a multi-level index and want to flatten it.
When you want to start fresh with a simple 0-based index after some data changes.
Syntax
Pandas
DataFrame.reset_index(level=None, drop=False, inplace=False, col_level=0, col_fill='')
If drop=True, the old index is removed instead of added as a column.
If inplace=True, the DataFrame is changed directly without creating a new one.
Examples
Resets the index and moves the old index into a new column.
Pandas
df.reset_index()
Resets the index and removes the old index without adding it as a column.
Pandas
df.reset_index(drop=True)
Resets the index and updates the original DataFrame directly.
Pandas
df.reset_index(inplace=True)
Sample Program
This example sets the 'Name' column as the index, then resets it back to a normal column.
Pandas
import pandas as pd

data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df = df.set_index('Name')
print('Before reset_index:')
print(df)

reset_df = df.reset_index()
print('\nAfter reset_index:')
print(reset_df)
OutputSuccess
Important Notes
Resetting index is useful after filtering rows to keep the index clean and simple.
Using inplace=True saves memory by modifying the DataFrame directly.
If your DataFrame has multiple index levels, you can reset specific levels using the level parameter.
Summary
Resetting index turns the current index into a normal column and creates a new default index.
Use drop=True to remove the old index instead of keeping it as a column.
You can reset the index in place or create a new DataFrame with the reset index.