Pandas DataFrame.reset_index: Reset Index of a DataFrame


Pandas DataFrame.reset_index

The DataFrame.reset_index method in pandas is used to reset the index of a DataFrame, replacing it with the default integer index. If the DataFrame has a MultiIndex, this method can remove one or more levels.


Syntax

The syntax for DataFrame.reset_index is:

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

Here, DataFrame refers to the pandas DataFrame whose index is being reset.


Parameters

ParameterDescription
levelSpecifies the index levels to remove. Can be an integer, string, tuple, or list. If None, all levels are removed.
dropIf True, removes the index without adding it as a column in the DataFrame.
inplaceIf True, modifies the DataFrame in place. Otherwise, returns a new DataFrame.
col_levelDetermines the level in the column hierarchy where labels are inserted when the DataFrame has multiple column levels.
col_fillDefines how the other levels are named when inserting the index into columns. Default is an empty string.
allow_duplicatesIf True, allows duplicate column labels to be created. Introduced in pandas 1.5.0.
namesSpecifies a name for the column(s) created from the index when reset. If using a MultiIndex, this must be a list of the same length as the number of levels.

Returns

A new DataFrame with the reset index, or None if inplace=True.


Examples

Resetting the Index of a DataFrame

By default, reset_index converts the existing index into a column and sets a new default integer index.

Python Program

import pandas as pd

# Create a DataFrame with a custom index
df = pd.DataFrame({'A': [10, 20, 30], 'B': [100, 200, 300]}, index=['x', 'y', 'z'])
print("Original DataFrame:")
print(df)

# Reset index
df_reset = df.reset_index()
print("\nDataFrame after reset_index:")
print(df_reset)

Output

Original DataFrame:
     A    B
x   10  100
y   20  200
z   30  300

DataFrame after reset_index:
  index   A    B
0     x  10  100
1     y  20  200
2     z  30  300

Resetting Index Without Adding Index as a Column

By setting drop=True, the index is removed without being added as a column.

Python Program

import pandas as pd

# Create a DataFrame with a custom index
df = pd.DataFrame({'A': [10, 20, 30], 'B': [100, 200, 300]}, index=['x', 'y', 'z'])

# Reset index and drop the existing index
df_reset_drop = df.reset_index(drop=True)
print(df_reset_drop)

Output

    A    B
0  10  100
1  20  200
2  30  300

Reset Index of a MultiIndex DataFrame

For MultiIndex DataFrames, reset_index removes one or more levels.

Python Program

# Create a MultiIndex DataFrame
arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]
index = pd.MultiIndex.from_arrays(arrays, names=('Group', 'Number'))
df_multi = pd.DataFrame({'Values': [100, 200, 300, 400]}, index=index)
print("Original MultiIndex DataFrame:")
print(df_multi)

# Reset MultiIndex
df_reset_multi = df_multi.reset_index()
print("\nDataFrame after reset_index:")
print(df_reset_multi)

Output

Original MultiIndex DataFrame:
                Values
Group Number        
A     1        100
      2        200
B     1        300
      2        400

DataFrame after reset_index:
  Group  Number  Values
0     A       1     100
1     A       2     200
2     B       1     300
3     B       2     400

Reset Index In-Place

Using inplace=True modifies the DataFrame directly instead of returning a new one.

Python Program

# Create a MultiIndex DataFrame
arrays = [['A', 'A', 'B', 'B'], [1, 2, 1, 2]]
index = pd.MultiIndex.from_arrays(arrays, names=('Group', 'Number'))
df_multi = pd.DataFrame({'Values': [100, 200, 300, 400]}, index=index)
print("Original MultiIndex DataFrame:")
print(df_multi)

# Reset MultiIndex
df_reset_multi = df_multi.reset_index()

# Reset index in-place
df.reset_index(inplace=True)
print(df)

Output

  index   A    B
0     x  10  100
1     y  20  200
2     z  30  300

Renaming the Index Column After Reset

Using the names parameter, the new column containing the previous index can be renamed.

Python Program

# Rename the index column
df_reset_rename = df.reset_index(names='Custom_Index')
print(df_reset_rename)

Output

  Custom_Index   A    B
0           x  10  100
1           y  20  200
2           z  30  300

Summary

In this tutorial, we explored the DataFrame.reset_index method in pandas. Key takeaways include:

  • By default, reset_index moves the current index into a new column and assigns a default integer index.
  • Setting drop=True removes the index without adding it as a column.
  • MultiIndex DataFrames can have one or more levels removed.
  • Using inplace=True modifies the DataFrame directly.
  • The names parameter allows renaming the column containing the previous index.

Python Libraries