What is the Right Syntax When Using .notnull() in Pandas?
You can apply multiple 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())]
A B C
0 1.0 1.0 1.0
Alternatively, you can just drop all columns which contain NaN
. The original DataFrame is not modified, instead a copy is returned.
df.dropna()
You can first select subset of columns by df[['A','B','C']]
, then apply notnull
and specify if all
values in mask are True
:
print (df[['A','B','C']].notnull())
A B C
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)])
A B C
0 1.0 1.0 1.0
Another solution is from Ayhan
comment with dropna
:
print (df.dropna(subset=['A', 'B', 'C']))
A B C
0 1.0 1.0 1.0
what is same as:
print (df.dropna(subset=['A', 'B', 'C'], how='any'))
and means drop all rows, where is at least one NaN
value.
You can simply do:
df.dropna()