Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace empty string with with np.nan but got 'NaN'

Tags:

pandas

numpy

I tried to replace empty string with np.nan

But I got None in cells , what wrong with that ?

Thanks

df.replace('', np.nan)

enter image description here

like image 705
newBike Avatar asked Oct 30 '22 16:10

newBike


1 Answers

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 Nones 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.

like image 93
Imanol Luengo Avatar answered Jan 04 '23 14:01

Imanol Luengo