0

How can I convert following pandas series to floats:

s = pd.Series(['2,3', 5.1, '', '3.2'])

I tried s.to_float() but got a ValueError: Unable to parse string "2,3" at position 0

ronnydw
  • 923
  • 16
  • 24

1 Answers1

1

Cast the Series to str if necessary, then str.replace the comma with a decimal, then you can use to_numeric:

In[24]:
s = pd.to_numeric(s.astype(str).str.replace(',','.'))
s

Out[24]: 
0    2.3
1    5.1
2    NaN
3    3.2
dtype: float64

If the dtype of the Series is already str then the astype(str) step can be skipped, but I'm assuming you have a more complicated real dataset issue

EdChum
  • 376,765
  • 198
  • 813
  • 562