简体   繁体   中英

Pandas change Nan Columns values to True or False

I need to change a column to either True or False based on the NaN value.

Here is the df.

    missing

0      NaN
1      b
2      NaN
4      y
5      NaN

would become

    missing

0      False
1      True
2      False
4      True
5      False

yes I can do a loop but there was to be a simple way to do in a single line of code.

thank you.

You can do

df['missing'].notna() # or notnull()

you need to overwrite the column values with binary applied on the same column, which can be achieved notna()

df['missing'] = df['missing'].notna()

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