I want to replace string boolean type present inside a column with actual boolean values.
kdf = pd.DataFrame(data={'col1' : [True, 'True', np.nan], 'dt': [datetime.now(), ' 2018-12-12', '2019-12-12'], 'bool':
[False, True, True], 'bnan': [False, True, np.nan]})
so here, I want to convert True(index 1 on col1) to actual boolean type True. What I did was,
kdf.loc[kdf['col1'].str.contains('true', na=False, case=False)] = True
kdf.loc[kdf['col1'].str.contains('false', na=False, case=False)] = False
which converts the column values to actual type but I'm in need of creating a function which accepts only the df column, do an in-line replace and return the modified column (like col.fillna). Note that we are not allowed to pass the whole df into that func. So I can't use df.loc.
Also I'm bit worry about performance, is there anyother way?