python - Replacing blank values (white space) with NaN in pandas - Stack Overflow

PHOTO EMBED

Sun Aug 23 2020 10:16:43 GMT+0000 (Coordinated Universal Time)

Saved by @arielvol #python

df = pd.DataFrame([
    [-0.532681, 'foo', 0],
    [1.490752, 'bar', 1],
    [-1.387326, 'foo', 2],
    [0.814772, 'baz', ' '],     
    [-0.222552, '   ', 4],
    [-1.176781,  'qux', '  '],         
], columns='A B C'.split(), index=pd.date_range('2000-01-01','2000-01-06'))

# replace field that's entirely space (or empty) with NaN
print(df.replace(r'^\s*$', np.nan, regex=True))
content_copyCOPY

https://stackoverflow.com/questions/13445241/replacing-blank-values-white-space-with-nan-in-pandas