Miyashita Hikaru - 9 months ago 40

Python Question

I would like to convert string (%) to float.but my method didnt work well.

the result slightly differ from correct number.

for example,

`a=pd.Series(data=["0.1%","0.2%"])`

0 0.1%

1 0.2%

dtype: object

first, I strip "%"

`a.str.rstrip("%")`

0 0.1

1 0.2

dtype: object

I tried to convert to numeric, but the result is strange.

I guess this phenomena come from binary digit system...

`pd.to_numeric(a.str.rstrip("%"))`

0 0.10000000000000000555

1 0.20000000000000001110

dtype: float64

and of course I couldnt convert % to numeric.

`pd.to_numeric(a.str.rstrip("%"))/100`

0 0.00100000000000000002

1 0.00200000000000000004

dtype: float64

I also tried .astype(float) method. but The result was same..

why this phenomena happen ? and how can I avoid this phenomena

Answer

Many rational numbers can't be represented exactly as a floating-point number. In particular, any number that has to have a five as a factor in the denominator, like 1/(2*5), can't be represented exactly. There isn't much you can do about this: either round the displayed number so it looks right, or use an infinite-precision library or a rational-numbers library. Here's a basic way to round the displayed number:

`>>> print "%.20f" % 0.1`

`0.10000000000000000555`

`>>> print "%.4f" % 0.1`

`0.1000`

Source (Stackoverflow)