What is the correct syntax when using .notnull () in Pandas?

I want to use .notnull() in multiple columns of a data block to exclude rows containing "NaN" values.

Let's say I have the following df :

  ABC 0 1 1 1 1 1 NaN 1 2 1 NaN NaN 3 NaN 1 1 

I tried to use this syntax but it does not work? Do you know what I am doing wrong?

 df[[df.A.notnull()],[df.B.notnull()],[df.C.notnull()]] 

I get this error:

 TypeError: 'Series' objects are mutable, thus they cannot be hashed 

What to do to get the following conclusion?

  ABC 0 1 1 1 

Any idea?

+11
source share
3 answers

First you can select a subset of the columns using df[['A','B','C']] , then apply notnull and indicate whether all values ​​in the True mask are:

 print (df[['A','B','C']].notnull()) ABC 0 True True True 1 True False True 2 True False False 3 False True True print (df[['A','B','C']].notnull().all(1)) 0 True 1 False 2 False 3 False dtype: bool print (df[df[['A','B','C']].notnull().all(1)]) ABC 0 1.0 1.0 1.0 

Another solution from Ayhan comment with dropna :

 print (df.dropna(subset=['A', 'B', 'C'])) ABC 0 1.0 1.0 1.0 

what is the same as

 print (df.dropna(subset=['A', 'B', 'C'], how='any')) 

and means deleting all rows where there is at least one NaN value.

+14
source

You can apply several conditions by combining them with the & operator (this works not only for the notnull() function).

 df[(df.A.notnull() & df.B.notnull() & df.C.notnull())] ABC 0 1.0 1.0 1.0 

Alternatively, you can simply delete all columns containing NaN . The original DataFrame is not changed; a copy is returned instead.

df.dropna()

+1
source

You can simply do:

 df.dropna() 
0
source

All Articles