I tried to replace empty string with np.nan
But I got None
in cells , what wrong with that ?
Thanks
df.replace('', np.nan)
There must be a problem with your data, and not with pandas. See bellow an example:
>>> data = [['a', 'b', ''], ['', 'e', 'f']]
>>> df = pd.DataFrame(data)
>>> df
0 1 2
0 a b
1 e f
If you try to replace ''
with np.nan
in the above example, you get the desired result:
>>> df.replace('', np.nan)
0 1 2
0 a b NaN
1 NaN e f
However, if by any chance your data was not correctly specified:
>>> data = [['a', 'b', None], [None, 'e', 'f']]
>>> df = pd.DataFrame(data)
>>> df
0 1 2
0 a b None
1 None e f
pandas
cannot help, as your data does not contain empty strings, but non-specified values (None
):
>>> df.replace('', np.nan)
0 1 2
0 a b None
1 None e f
However, there is still a chance you can remove the None
s from your table. The following, as silly as it sounds, should still work:
>>> df.replace(np.nan, np.nan)
0 1 2
0 a b NaN
1 NaN e f
It is hard to say without having your raw data, but hope the above helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With