Reputation: 850
I have a dataFrame
df = pd.DataFrame({'k':['a','b','c'],'v':[1,None,3]})
k v
0 a 1.0
1 b NaN
2 c 3.0
I want to check if column k
's b
value is NaN or not
I have tried
if df['v'][df['k']=='b'] is None:
print("yes")
else:
print('no')
this gives output no
I have also tried
if df['v'][df['k']=='b'] == None:
print("yes")
else:
print('no')
and
if df['v'][df['k']=='b'].isna():
print("yes")
else:
print('no')
both give me this error
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Upvotes: 1
Views: 2926
Reputation: 18377
If, as long a you have a null value, you would want to return the True
then use .any()
. If you want all values to be Null (in this case it doesn't matter because there's a single value) then use .all()
:
if df[df['k']=='b']['v'].isna().any():
print("yes")
else:
print('no')
Output:
yes
Upvotes: 1