Question

I have been checking each value of each row and if all of them are null, I delete the row with something like this:

df = pandas.concat([df[:2], df[3:]])

But, I am thinking there's got to be a better way to do this. I have been trying to use a mask or doing something like this:

rows_to_keep  = df.apply(
    lambda row : 
      any([if val is None for val in row ])
   , axis=1) 

I also tried something like this (suggested on another stack overflow question)

pandas.DataFrame.dropna()

but don't see any differences in my printed dataframe.

Was it helpful?

Solution

dropna returns a new DataFrame, you probably just want:

df = df.dropna()

or

df.dropna(inplace=True)

If you have a more complicated mask, rows_to_keep, you can do:

df = df[rows_to_keep]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top