简体   繁体   中英

Python - checking if value in dataframe in nan in an if statement

I am trying to create a column that tells whether each row in another column is NaN or not.

if top20X['cc_gb_15'].isna() == True:
    top20X['cc_gb_15_null'] = 1
else:
    0

However, the code trips up on the first line and I receive the following error:

"The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all()."

Funnily enough top20X['cc_gb_15'].isna() works just fine in telling me whether that row is NaN or not, but when i try to found out if its NaN it won't do it.

import math
df['cc_gb_15_null']=df['cc_gb_15'].apply(lambda x: 1 if x == math.nan else 0)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM