0
0
PandasHow-ToBeginner · 3 min read

How to Reset Index in pandas: Syntax and Examples

To reset the index in pandas, use the reset_index() method on a DataFrame. This method moves the current index into a column and creates a new default integer index. Use drop=True to discard the old index instead of keeping it as a column.
📐

Syntax

The basic syntax of reset_index() is:

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

Explanation:

  • level: Specify which index level(s) to reset (for MultiIndex).
  • drop: If True, the old index is removed instead of added as a column.
  • inplace: If True, modifies the DataFrame in place without returning a new one.
  • col_level and col_fill: Used for MultiIndex columns to control placement of new columns.
python
df.reset_index(drop=False, inplace=False)
💻

Example

This example shows how to reset the index of a DataFrame. The original index becomes a new column unless drop=True is used.

python
import pandas as pd

# Create a sample DataFrame with a custom index
data = {'Name': ['Alice', 'Bob', 'Charlie'], 'Age': [25, 30, 35]}
df = pd.DataFrame(data)
df.index = ['a', 'b', 'c']

# Reset index without dropping the old index
reset_df = df.reset_index()

# Reset index and drop the old index
reset_df_drop = df.reset_index(drop=True)

print('Original DataFrame:')
print(df)
print('\nReset index (old index as column):')
print(reset_df)
print('\nReset index with drop=True:')
print(reset_df_drop)
Output
Original DataFrame: Name Age a Alice 25 b Bob 30 c Charlie 35 Reset index (old index as column): index Name Age 0 a Alice 25 1 b Bob 30 2 c Charlie 35 Reset index with drop=True: Name Age 0 Alice 25 1 Bob 30 2 Charlie 35
⚠️

Common Pitfalls

Common mistakes when resetting index include:

  • Forgetting to use drop=True when you don't want the old index as a column, which can add unwanted columns.
  • Not using inplace=True if you want to modify the original DataFrame directly.
  • Resetting index on a MultiIndex without specifying the level can reset all levels unintentionally.
python
import pandas as pd

df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
df.index = ['x', 'y']

# Wrong: forget drop=True, old index becomes a column
wrong = df.reset_index()

# Right: drop old index if not needed
right = df.reset_index(drop=True)

print('Wrong reset:')
print(wrong)
print('\nRight reset:')
print(right)
Output
Wrong reset: index A B 0 x 1 3 1 y 2 4 Right reset: A B 0 1 3 1 2 4
📊

Quick Reference

ParameterDescriptionDefault
levelIndex level(s) to reset (for MultiIndex)None (all levels)
dropRemove old index instead of adding as columnFalse
inplaceModify DataFrame in placeFalse
col_levelFor MultiIndex columns, level to insert new columns0
col_fillFor MultiIndex columns, value to fill missing labels''

Key Takeaways

Use df.reset_index() to move the current index into a column and create a new default index.
Set drop=True to remove the old index instead of keeping it as a column.
Use inplace=True to modify the original DataFrame without creating a copy.
For MultiIndex, specify level to reset specific index levels.
Always check your DataFrame after resetting index to avoid unexpected columns.