Replace the empty string with np.nan, but get "NaN"

I tried replacing an empty string with np.nan

But I got Nonein the cells, what's wrong with that?

thanks

df.replace('', np.nan)

enter image description here

+4
source share
1 answer

There should be a problem with your data, not pandas. Example below:

>>> 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.nanin the above example, you will get the desired result:

>>> df.replace('', np.nan)
     0  1    2
0    a  b  NaN
1  NaN  e    f

However, if you do not accidentally provide your details:

>>> data = [['a', 'b', None], [None, 'e', 'f']]
>>> df = pd.DataFrame(data)
>>> df
      0  1     2
0     a  b  None  
1  None  e     f

pandascannot help, since your data does not contain empty lines, but no given values ​​( None):

>>> df.replace('', np.nan) 
      0  1     2
0     a  b  None
1  None  e     f

However, there is still a chance that you can remove Nonefrom your table. The following, as dumb as it seems, should work:

>>> df.replace(np.nan, np.nan)
     0  1    2
0    a  b  NaN
1  NaN  e    f

, , , .

+1

Source: https://habr.com/ru/post/1615026/


All Articles