I have below pandas dataframe df:
| clm1 | clm2|
| 79.02 | 80.98|
| 78.55 | 81.47|
| 98.99 | 101.01|
| 999.54 | 999.55|
| 999.55 | 999.55|
I am performing below calculation on it:
df['avg'] = (df['clm1']+df['clm2'])/2
print(df)
| clm1 | clm2 | avg |
|79.02 | 80.98 | 80.000 |
|78.55 | 81.47 | 80.010 |
|98.99 | 101.01 |100.000 |
|99.54 | 999.55 |999.545 |
|99.55 | 999.55 |999.550 |
When I am writing the above dataframe to csv I am getting incorrect result.
df.to_csv(myfile.csv)
clm1 , clm2 , avg
79.02 , 80.98 , 80.0
78.55 , 81.47 , 80.00999999999999 *# This should be 80.01*
98.99 , 101.01, 100.0
999.54, 999.55, 999.545
999.55, 999.55, 999.55
I understand the issues with floating point and i have gone through below Answers:
Python float - str - float weirdness Is floating point math broken? These suggest to use Decimal instead of float. But I am not able to find how to do that. Note: I do not want to use any rounding of method. I need the exact result.