0 points

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


dashboard

Sun Aug 23 2020 10:15:29 GMT+0000 (UTC)

Posted 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_copy Copy

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