1

I was trying to remove the rows with nan values in a python dataframe and when i do so, i want the row identifiers to shift in such way that the identifiers in the new data frame start from 0 and are one number away from each other. By identifiers i mean the numbers at the left of the following example. Notice that this is not an actual column of my df. This is rather placed by default in every dataframe.

If my Df is like:

        name   toy   born
    0   a      1    2020
    1   na     2    2020
    2   c      5    2020
    3   na     1    2020
    4   asf    1    2020

i want after dropna()

        name   toy   born
    0   a      1    2020
    1   c      5    2020
    2   asf    1    2020

I dont want, but this is what i get:

        name   toy   born
    0   a      1    2020
    2   c      5    2020
    4   asf    1    2020
pltc
  • 5,836
  • 1
  • 13
  • 31
Mangostino
  • 157
  • 1
  • 6
  • 1
    Related: [how to reset index pandas dataframe after dropna() pandas dataframe](https://stackoverflow.com/q/40755680/15497888) / [How to reset index in a pandas dataframe?](https://stackoverflow.com/q/20490274/15497888) `df = df.dropna().reset_index(drop=True)` – Henry Ecker Oct 23 '21 at 22:32

2 Answers2

1

You can simply add df.reset_index(drop=True)

Alessandro
  • 361
  • 1
  • 9
  • 1
    Good answer, but I think it's helpful to include a link to the Pandas documentation so that the asker can understand why your solution works. For example: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.reset_index.html – Nick ODell Oct 23 '21 at 22:29
0

By default, df.dropna and df.reset_index are not performed in place. Therefore, the complete answer would be as follows.

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

Results and Explanations

The above code yields the following result.

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

  name  toy  born
0    a    1  2020
1    c    5  2020
2  asf    1  2020

We use the argument drop=True to drop the index column. Otherwise, the result would look like this.

>>> df = df.dropna().reset_index()
>>> df

   index name  toy  born
0      0    a    1  2020
1      2    c    5  2020
2      4  asf    1  2020
Troll
  • 1,895
  • 3
  • 15
  • 34