Home » Pandas: How to Reset Index After Using dropna()

Pandas: How to Reset Index After Using dropna()

by Erma Khan

You can use the following basic syntax to reset an index of a pandas DataFrame after using the dropna() function to remove rows with missing values:

df = df.dropna().reset_index(drop=True)

The following example shows how to use this syntax in practice.

Example: Reset Index in Pandas After Using dropna()

Suppose we have the following pandas DataFrame that contains information about various basketball players:

import pandas as pd
import numpy as np

#create dataFrame
df = pd.DataFrame({'team': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                   'points': [18, np.nan, 19, 14, 14, 11, 20, 28],
                   'assists': [5, 7, 7, 9, 12, np.nan, 9, 4],
                   'rebounds': [11, 8, 10, 6, 6, 5, np.nan, 12]})

#view DataFrame
print(df)

  team  points  assists  rebounds
0    A    18.0      5.0      11.0
1    B     NaN      7.0       8.0
2    C    19.0      7.0      10.0
3    D    14.0      9.0       6.0
4    E    14.0     12.0       6.0
5    F    11.0      NaN       5.0
6    G    20.0      9.0       NaN
7    H    28.0      4.0      12.0

Now suppose we use the dropna() function to drop all rows from the DataFrame that have a missing value in any column:

#drop rows with nan values in any column
df = df.dropna()

#view updated DataFrame
print(df)

  team  points  assists  rebounds
0    A    18.0      5.0      11.0
2    C    19.0      7.0      10.0
3    D    14.0      9.0       6.0
4    E    14.0     12.0       6.0
7    H    28.0      4.0      12.0

Notice that the index still contains the original index values for each row.

To reset the index after using the dropna() function, we can use the following syntax:

#drop rows with nan values in any column
df = df.dropna().reset_index(drop=True)

#view updated DataFrame
print(df)

  team  points  assists  rebounds
0    A    18.0      5.0      11.0
1    C    19.0      7.0      10.0
2    D    14.0      9.0       6.0
3    E    14.0     12.0       6.0
4    H    28.0      4.0      12.0

Notice that each of the rows with missing values have been removed and the index values have been reset.

The index values now range from 0 to 4.

Additional Resources

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

How to Print Pandas DataFrame with No Index
How to Filter by Index Value in Pandas
How to Use First Column as Index in Pandas

Related Posts