How to Reset an Index in Pandas DataFrame (With Examples)


You can use the following syntax to reset an index in a pandas DataFrame:

df.reset_index(drop=True, inplace=True)

Note the following arguments:

  • drop: Specifying True prevents pandas from saving the original index as a column in the DataFrame.
  • inplace: Specifying True allows pandas to replace the index in the original DataFrame instead of creating a copy of the DataFrame.

The following examples show how to use this sytnax in practice.

Example 1: Reset Index & Drop Old Index

Suppose we have the following pandas DataFrame:

import pandas as pd

#define DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]},
                   index=[0, 4, 3, 5, 2, 1, 7, 6])

#view DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
4      12        7         8
3      15        7        10
5      14        9         6
2      19       12         6
1      23        9         5
7      25        9         9
6      29        4        12

The following code shows how to reset the index of the DataFrame and drop the old index completely:

#reset index
df.reset_index(drop=True, inplace=True)

#view updated DataFrame
print(df)

   points  assists  rebounds
0      25        5        11
1      12        7         8
2      15        7        10
3      14        9         6
4      19       12         6
5      23        9         5
6      25        9         9
7      29        4        12

Notice that the index has been reset and the values in the index now range from 0 to 7.

Example 2: Reset Index & Retain Old Index as Column

Suppose we have the following pandas DataFrame:

import pandas as pd

#define DataFrame
df = pd.DataFrame({'points': [25, 12, 15, 14, 19, 23, 25, 29],
                   'assists': [5, 7, 7, 9, 12, 9, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, 9, 12]},
                   index=['A', 'C', 'D', 'B', 'E', 'G', 'F', 'H'])

#view DataFrame
print(df)

   points  assists  rebounds
A      25        5        11
C      12        7         8
D      15        7        10
B      14        9         6
E      19       12         6
G      23        9         5
F      25        9         9
H      29        4        12

The following code shows how to reset the index of the DataFrame and retain the old index as a column in the DataFrame:

#reset index and retain old index as a column
df.reset_index(inplace=True)

#view updated DataFrame
print(df)

  index  points  assists  rebounds
0     A      25        5        11
1     C      12        7         8
2     D      15        7        10
3     B      14        9         6
4     E      19       12         6
5     G      23        9         5
6     F      25        9         9
7     H      29        4        12

Notice that the index has been reset and the values in the index now range from 0 to 7.

Also notice that the old index (with letters) is retained as a new column in the DataFrame called ‘index.’

Additional Resources

The following tutorials explain how to perform other common operations in pandas:

How to Convert Index to Column in Pandas
How to Set Column as Index in Pandas
How to Rename Index in Pandas

Leave a Reply

Your email address will not be published.