简体   繁体   中英

how to Handle missing values in bool column in python?

I have a logical column

df['Employed'].dtypes
Out[3]: dtype('O')

values showing

df['Employed'].value_counts()
Out[4]:
False    156133
True      13271
Name: Self_Employed2, dtype: int64

unique showing nan

df['Employed'].unique()
Out[5]:array([nan, False, True], dtype=object)

Number of missing

df['Employed'].isnull().sum()
Out[6]: 21210

I am trying to convert logical to string and change 'nan' to 'False', then Change 'False' to 'No' and 'True' to 'Yes', Triied to convert 'nan' as 'False' using fillna(False), its not working Tried using str.replace('False','No') that's also not working

I need

Out[7]:
False    177343
True      13271
Name: Employed, dtype: int64

You can use try replace missing values by Series.fillna with False without ' for boolean:

df.Employed = df.Employed.fillna(False)

Or remove missing values by Series.dropna :

df.Employed = df.Employed.dropna()

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