Reputation: 151
I'm having a bad time converting some numerical precision numbers to CSV in pandas.
I have to be as precise as I can in this conversion but I don't know if pandas is wrong or my formatting is not adequate.
I already have tried to mix some different float_formats from the range 10 to 20 but I don't know if this is the way.
import pandas as pd
import numpy as np
data = []
for i in range(0,20):
data.append({
'number': 123456789*(10**-i)
})
df = pd.DataFrame(data,dtype=np.float64)
print(df.to_csv(index=False,float_format="%.20f"))
Output:
number
123456789.00000000000000000000
12345678.90000000037252902985
1234567.89000000013038516045
123456.78900000000430736691
12345.67890000000079453457
1234.56789000000003397872
123.45678900000000055570
12.34567889999999934503
1.23456789000000011214
0.12345678900000001121
0.01234567890000000043
0.00123456788999999991
0.00012345678900000000
0.00001234567890000000
0.00000123456789000000
0.00000012345678900000
0.00000001234567890000
0.00000000123456789000
0.00000000012345678900
0.00000000001234567890
As you can see, the 2nd number is having a lot of non-sense numbers after the .9000 (several zeros here). Is there a way to avoid having these numbers on my CSV?
My main goal is to have only zeros after the dot.
Expected Output:
number
123456789.00000000000000000000
12345678.900000000000000000000
1234567.8900000000000000000000
123456.78900000000000000000000
12345.6789000000000000000
1234.56789000000000000000
123.45678900000000000000
. . .
0.00012345678900000000
0.00001234567890000000
0.00000123456789000000
0.00000012345678900000
0.00000001234567890000
0.00000000123456789000
0.00000000012345678900
0.00000000001234567890
Am I being stupid or this is really a no-win scenario?
Upvotes: 0
Views: 756
Reputation: 164
As long as you don't actually need to have some amount of zeros after the last significant digit, then changing the last line to:
print(df.to_csv(index=False,float_format="%.10g")
Gives you only 10 significant digits, so the output is:
number
123456789
12345678.9
1234567.89
123456.789
12345.6789
1234.56789
123.456789
12.3456789
1.23456789
0.123456789
0.0123456789
0.00123456789
0.000123456789
1.23456789e-05
Upvotes: 1