0

I have a pandas Series that is populated with floats. When I convert the pandas series to a list later on some of the floats are in scientific notation. There are lots of posts on stackoverflow which changes the display of pandas float which I totally understand, but I need these values all to be in decimal format when I call the .tolist() method.

s = pd.Series([-3.75e-05,
 -6.25e-05,
 -8.75e-05,
 -8.75e-05,
 -8.75e-05,
 -0.0001125,
 -0.00015,
 -0.0001625,
 -0.0001625,
 -0.0001625])

Why are some floating point numbers being printed with scientific notation and some are using decimal?

Cr1064
  • 409
  • 5
  • 15

3 Answers3

0

Try using Decimal, and the result will be a list :

from decimal import Decimal
s = pd.Series([-3.75e-05,
 -6.25e-05,
 -8.75e-05,
 -8.75e-05,
 -8.75e-05,
 -0.0001125,
 -0.00015,
 -0.0001625,
 -0.0001625,
 -0.0001625])


result = [round(Decimal(x),5) for x in s]
result

Result :

[Decimal('-0.00004'),
 Decimal('-0.00006'),
 Decimal('-0.00009'),
 Decimal('-0.00009'),
 Decimal('-0.00009'),
 Decimal('-0.00011'),
 Decimal('-0.00015'),
 Decimal('-0.00016'),
 Decimal('-0.00016'),
 Decimal('-0.00016')]
J.K
  • 1,178
  • 10
  • 13
  • 1
    Might be worth throwing some rounding in there, as this has decimal precision issues compared to the original value, and OP stated they're passing it to a gui – G. Anderson Sep 09 '19 at 17:25
  • @G.Anderson, I added the round. In fact, I am not sure how "Cr1064" wants the output :) – J.K Sep 09 '19 at 19:14
0

One thing I'd like to point out is that values in scientific notation won't have any major effect on analysis. J.K gave the best response to the problem but the approach(non-vectorized approach) can be too overwhelming for the system as the size of the dataset increases, also the 'result' variable in his/her example has a data type of Decimal(not float).

>>>type(decimal.Decimal(1.22))
<class 'decimal.Decimal'>

To overcome these issues, you can do this:

import decimal
s = pd.Series([-3.75e-05,
 -6.25e-05,
 -8.75e-05,
 -8.75e-05,
 -8.75e-05,
 -0.0001125,
 -0.00015,
 -0.0001625,
 -0.0001625,
 -0.0001625])
s=s.apply(decimal.Decimal)
print(s)

Output:

0    -0.0000374999999999999967148674173689215649574...
1    -0.0000625000000000000013010426069826053208089...
2    -0.0000874999999999999991109542185618863641138...
3    -0.0000874999999999999991109542185618863641138...
4    -0.0000874999999999999991109542185618863641138...
5    -0.0001124999999999999969208658301411674074188...
6    -0.0001499999999999999868594696694756862598296...
7    -0.0001624999999999999925406890532997294940287...
8    -0.0001624999999999999925406890532997294940287...
9    -0.0001624999999999999925406890532997294940287...
-1

If you are not worried about speed,

def to_str(x): return '%f' % x
s=s.apply(to_str)
nily
  • 67
  • 1
  • 8