Monday, November 14, 2022

AI/ML Dataframe how to replace string with another in column

 df2 = pd.DataFrame([

    [-0.532681, 'foo', 0],

    [1.490752, 'bar', 1],

    [-1.387326, 'foo', 2],

    [0.814772, 'baz', ' '],     

    [-0.222552, '   ', 4],

    [-1.176781,  'qux', '  '],         

],columns=['one', 'two', 'three'])

# df2

df2['two'] = df2['two'].replace(r'^\s*$', np.nan, regex=True)

df2


If we have to do the same in entire dataframe, just do this below 

df2 = df2.replace(r'^\s*$', np.nan, regex=True)



No comments:

Post a Comment