15.1 C
London
Friday, July 5, 2024
HomePandas in PythonDataFrame Functions in PythonHow to Reset an Index in Pandas DataFrame (With Examples)

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

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories