Pandas reset_index()

The reset_index() method in Pandas resets the index of a DataFrame.

Example

import pandas as pd

# create a dictionary
data = {'Name': ['John', 'Doe', 'Jane'], 'Age': [28, 30, 25]}

# convert the dictionary to a DataFrame with a custom index
df = pd.DataFrame(data, index=['one', 'two', 'three'])

# reset the index of the DataFrame df_reset = df.reset_index()
# display the DataFrame after resetting the index print(df_reset) ''' Output index Name Age 0 one John 28 1 two Doe 30 2 three Jane 25 '''

reset_index() Syntax

The syntax of the reset_index() method in Pandas is:

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

reset_index() Arguments

The reset_index() function takes following arguments:

  • level - specifies which levels of the index to reset
  • drop - specifies whether to discard index or not
  • inplace - determines whether to modify the original object directly or return a new modified object.
  • col_level - specifies which level to insert the reset index labels into if columns have multiple levels

reset_index() Return Value

The reset_index() method returns a new DataFrame with a reset index.


Example 1: Reset the Index of DataFrame

import pandas as pd

# create a sample DataFrame 
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [25, 30, 35, 40]
}, index=[101, 102, 104, 105])

# print the original DataFrame
print("Original DataFrame:")
print(df)
print("\n")  

# reset the index of the DataFrame df_reset = df.reset_index()
# display the DataFrame after resetting the index print("DataFrame after reset_index():") print(df_reset)

Output

Original DataFrame:
        Name  Age
101    Alice   25
102      Bob   30
104  Charlie   35
105    David   40


DataFrame after reset_index():
     index   Name  Age
0    101    Alice   25
1    102      Bob   30
2    104  Charlie   35
3    105    David   40

In the above example, we have created the df DataFrame containing names and ages of four individuals. And these entries are indexed with custom indices: 101, 102, 104, and 105.

Then we used the reset_index() method to reset the index of df.

Hence, in the new DataFrame, the custom indices are moved to the column named index, and a default integer index (0 to 3) was introduced.


Example 2: Reset Specific Level

import pandas as pd

# create a MultiIndex DataFrame
arrays = [
    ['A', 'A', 'B', 'B'],
    [1, 2, 1, 2]
]
index = pd.MultiIndex.from_arrays(arrays, names=('Letter', 'Number'))
df = pd.DataFrame({'Data': [10, 20, 30, 40]}, index=index)

print("Original DataFrame:")
print(df)
print("\n")

# reset the Number level df_reset_number = df.reset_index(level='Number')
print("After resetting the 'Number' level:") print(df_reset_number) print("\n")
# reset the Letter level df_reset_letter = df.reset_index(level='Letter')
print("After resetting the 'Letter' level:") print(df_reset_letter) print("\n")
# reset both Letter and Number levels df_reset_both = df.reset_index(level=['Letter', 'Number'])
print("After resetting both 'Letter' and 'Number' levels:") print(df_reset_both)

Output

Original DataFrame:
               Data
Letter Number      
A      1         10
       2         20
B      1         30
       2         40


After resetting the 'Number' level:
        Number  Data
Letter              
A            1    10
A            2    20
B            1    30
B            2    40


After resetting the 'Letter' level:
       Letter   Data
Number             
1           A    10
2           A    20
1           B    30
2           B    40


After resetting both 'Letter' and 'Number' levels:
    Letter  Number Data
0      A       1    10
1      A       2    20
2      B       1    30
3      B       2    40

Here,

  1. df.reset_index(level='Number') - we reset only the Number level, turning it into a regular column.
  2. df.reset_index(level='Letter') - we reset only the Letter level, turning it into a regular column.
  3. df.reset_index(level=['Letter', 'Number']) - we reset both levels, turning them into regular columns.

Example 3: Drop the Index Using reset_index()

import pandas as pd

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

# display original DataFrame
print("Original DataFrame:")
print(df)
print("\n")

# drop the index using reset_index(drop=True) df_reset = df.reset_index(drop=True)
# display the DataFrame after resetting the index print("DataFrame after dropping the index:") print(df_reset)

Output

Original DataFrame:
     Name    Age
a    Alice   25
b      Bob   30
c  Charlie   35
d    David   40


DataFrame after dropping the index:
     Name    Age
0    Alice   25
1      Bob   30
2  Charlie   35
3    David   40

In this example, we can see, after using reset_index(drop=True), the custom index 'a', 'b', 'c', 'd' has been discarded, and the DataFrame now has a default integer index starting from 0.


Example 4: Setting a New Column as Index

import pandas as pd

# create a sample DataFrame
df = pd.DataFrame({
    'ID': [101, 102, 103, 104],
    'Name': ['Alice', 'Bob', 'Charlie', 'David'],
    'Age': [25, 30, 35, 40]
})

# display the original DataFrame
print("Original DataFrame:")
print(df)
print("\n")

# set 'ID' column as the index df_set = df.set_index('ID')
# display the DataFrame after setting 'ID' as the index print("DataFrame after setting 'ID' as the index:") print(df_set)

Output

Original DataFrame:
   ID     Name  Age
0  101    Alice   25
1  102      Bob   30
2  103  Charlie   35
3  104    David   40

DataFrame after setting 'ID' as the index:
        Name  Age
ID               
101    Alice   25
102      Bob   30
103  Charlie   35
104    David   40

Here, we can see that the ID column has been set as the new index for the DataFrame.

The DataFrame no longer has a default integer index, and the ID column is no longer one of the DataFrame's columns, it's the index.


Example 5: Setting a New Column as Index

import pandas as pd

# create a DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [24, 30, 35]
}, index=['a', 'b', 'c'])

print("Original DataFrame:")
print(df)
print("\n")

# use of reset_index() with inplace=False (default)
new_df = df.reset_index()
print("DataFrame returned by reset_index() (inplace=False):")
print(new_df)
print("\n")
print("Original DataFrame remains unchanged:")
print(df)
print("\n")

# use of reset_index() with inplace=True df.reset_index(inplace=True)
print("Original DataFrame after reset_index() with inplace=True:") print(df)

Output

Original DataFrame:
      Name  Age
a    Alice   24
b      Bob   30
c  Charlie   35


DataFrame returned by reset_index() (inplace=False):
   index   Name    Age
0     a    Alice   24
1     b      Bob   30
2     c  Charlie   35


Original DataFrame remains unchanged:
      Name  Age
a    Alice   24
b      Bob   30
c   Charlie   35


Original DataFrame after reset_index() with inplace=True:
   index    Name  Age
0     a    Alice   24
1     b      Bob   30
2     c  Charlie   35

In this example, when

  1. reset_index() is used with its default behavior (inplace=False), it returns a new DataFrame with the index reset, and the original DataFrame remains unchanged.
  2. reset_index(inplace=True) is used, the original DataFrame is modified directly to reset the index, and nothing is returned by the function.

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges