Home > Mobile >  Remove from the beginning of DataFrame, all rows with at least one nan
Remove from the beginning of DataFrame, all rows with at least one nan

Time:10-13

For the following dataframe:

     a    b     c
0  NaN  5.0   NaN
1  2.0  6.0   NaN
2  3.0  7.0  11.0
3  4.0  NaN  12.0

I want to remove all rows with at least one NaN from the first row until a 'full' row is found. For the example above, rows 0 & 1 contain NaN so they are dropped. Row 2 is a 'full' row so it is retained, along with all following rows.

i.e., I want to get:


     a    b     c
2  3.0  7.0  11.0
3  4.0  NaN  12.0

How can I achieve this?

CodePudding user response:

Test non missing values per all rows by DataFrame.notna by DataFrame.all and Series.cummax and filter in boolean indexing:

df = df[df.notna().all(axis=1).cummax()]

print (df)
     a    b     c
2  3.0  7.0  11.0
3  4.0  NaN  12.0
  • Related