13

I have a Dataframe, i need to drop the rows which has all the values as NaN.

ID      Age    Gender
601     21       M
501     NaN      F
NaN     NaN      NaN

The resulting data frame should look like.

Id     Age    Gender
601     21      M
501    NaN      F

I used df.drop(axis = 0), this will delete the rows if there is even one NaN value in row. Is there a way to do as required?

Harshith
  • 303
  • 2
  • 5
  • 16

2 Answers2

23

The complete command is this:

df.dropna(axis = 0, how = 'all', inplace = True)

you must add inplace = True argument, if you want the dataframe to be actually updated. Alternatively, you would have to type:

df = df.dropna(axis = 0, how = 'all')

but that's less pythonic IMHO.

Leevo
  • 6,445
  • 3
  • 18
  • 52
2

This should do it:

df.dropna(axis = 0, how = 'all')
Stephen Rauch
  • 1,831
  • 11
  • 23
  • 34
Danny
  • 1,166
  • 1
  • 8
  • 16