I am trying to convert a column containing True/False and null values in string format to Boolean. But whatever I do I end up with either all True values or False Below is my approach to
consider following dataFrame
df = pd.DataFrame({'w':['True', np.nan, 'False'
'True', np.nan, 'False']})
df['w'].dtypes
Out: dtype('O')
df['w'].unique()
Out: array([True, nan, False], dtype=object)
d = {'nan': np.nan,'False':False, 'True': True}
df['w']=df['w'].map(d)
df['w'].dtypes
Out: dtype('O')
df['w'].unique()
array([nan], dtype=object)
One other approach I used is following this SO post:
d = {'nan': 0,'False':0, 'True': 1 }
df['w']=df['w'].map(d)
df['w']=df['w'].astype('bool')
Now it turns to bool but converts all values to True
df['w'].dtypes
Out: dtype('bool')
df['w'].unique()
Out: array([ True])
What am I doing wrong? I want all null values to be null