11

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
  • 283
  • 2
  • 5
  • 16

2 Answers2

21

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,225
  • 3
  • 16
  • 52
  • 3
    inplace is not recommended and will actually be removed in future versions: https://github.com/pandas-dev/pandas/issues/16529 – tdy Feb 03 '23 at 22:51
  • Good point. Obviously when I wrote this reply the use of inplace was not deprecated. – Leevo Feb 25 '23 at 10:53
  • @tdy Interesting that issue was from 2017 and in 2023 inplace has not been deprecated. – mp252 Nov 22 '23 at 15:34
  • 1
    @mp252 inplace has been inadvisable since 2017, but it won't start being "deprecated" (formally in the API) until the PDEP-8 proposal gets merged. – tdy Nov 23 '23 at 01:41
  • Good to know! Looks like I have got a lot of refactoring to do! – mp252 Nov 23 '23 at 15:55
2

This should do it:

df.dropna(axis = 0, how = 'all')
Stephen Rauch
  • 1,783
  • 11
  • 22
  • 34
Danny
  • 1,148
  • 1
  • 8
  • 16