Reputation: 2866
related to float64 with pandas to_csv (but different; here, it is an IEEE issue). an input csv file is
name,value
Alice,0.009
Bob,1.0
We want to write an output csv file that looks similar. of course, 0.009 is not easily storable as an IEEE number, where it really is 0.009000000000000001 .
Not a problem in computations, but now I want to save it nicely. With a float_format, there are too many digits being forced on every other value in the table. Without one, the file is painful for my consumers.
In [2]: df = pd.read_csv(orig)
...: df.to_csv(sys.stdout,float_format="%.5f")
,name,value
0,Alice,0.00900
1,Bob,1.00000
In [3]: df = pd.read_csv(orig)
...: df.to_csv(sys.stdout)
,name,value
0,Alice,0.009000000000000001
1,Bob,1.0
I could write a function that iterates over all floating point columns in the data frame ("add an epsilon, print at %.15f precision to a string column, then delete trailing zeros after the decimal point, then do the .csv.gz writing"). however, presumably this is a common wish, so it probably already exists somewhere, but I don't know how to find it?!
pointers appreciated.
Upvotes: 0
Views: 101
Reputation: 30589
df.to_csv(sys.stdout,float_format="%.15g")
Output:
,name,value
0,Alice,0.009
1,Bob,1
From the docs:
... insignificant trailing zeros are removed from the significand, and the decimal point is also removed if there are no remaining digits following it.
... The default precision is 6.
Upvotes: 1