0
0.03611642492570208

such numbers are present as string in a CSV file, I wish to read them and perform mathematical operations, but when I read it it is read as String and when i convert it to numeric form it is rounded off.

How can i convert it to numeric value without loosing precision.

Edited :

 item_id,deal_probability
 6544e41a8817,0.299918385137877
 65b9484d670f,0.09258187813010357
 8bab230b2ecd,0.15371873083249338
 8e348601fefc,0.07208665773638352
 8bd2fe400b89,0.25543690938853253
 c63dbd6c657f,0.05238764253800446
 6d1a410df86e,0.0512983797826358
 e8d3e7922b80,0.009989860172001194
 2bc1ab208462,0.04119998171932098

This is the format of my csv file and when I read it in my jupyter notebook the value under deal_probability is rounded off to 6 places after decimal.

Expected result :- I want to read the entire csv without any change in the value under deal_probability

ashunigion
  • 1,769
  • 1
  • 11
  • 15
  • 1
    Possible duplicate of [How do I parse a string to a float or int in Python?](https://stackoverflow.com/questions/379906/how-do-i-parse-a-string-to-a-float-or-int-in-python) – l'L'l Jun 22 '18 at 03:14
  • Have a look at the [decimal](https://docs.python.org/3.6/library/decimal.html) module. – Paul Panzer Jun 22 '18 at 03:16
  • I made the required changes to give detailed explaination – ashunigion Jun 22 '18 at 07:07
  • 6 decimal places is not how your data is being stored. It's being stored as `float`, I believe 64-bit floats should manager ~16 decimal place precision. Can you be more precise, do you need precision to *arbitrary* decimal points or is there a minimum? Calculations with `decimal.Decimal` are inefficient. – jpp Jul 10 '18 at 06:00

3 Answers3

1

Yes, you should be using decimals. In order to convert from a string to a decimal, you would first import decimal, and then pass in string to the Decimal function like so

from decimal import Decimal
myDecimal = Decimal("0.03611642492570208")

You can then perform operations on the decimal as you would with any other numerical type.

Keveloper
  • 773
  • 5
  • 10
1

The decimal module can be helpful for this - https://docs.python.org/3.6/library/decimal.html

from decimal import *
a = Decimal('0.03611642492570208')
print(a) # 0.03611642492570208
print(a + 1) # 1.03611642492570208
print(a + Decimal(1.1)) # 1.136116424925702168817841970
mike.k
  • 3,277
  • 1
  • 12
  • 18
1

We have nothing to worry about, as this is rounded down to 6 digits only for the purposes of screen display. If we save the file, we'll see that the digits are still there.

If we absolutely want to force pandas to read this column as number then we can do

import pandas as pd
import numpy as np

file = pd.read_csv('your_file_name.csv', dtype={'deal_probability': np.float64})

Credit: tilii

ashunigion
  • 1,769
  • 1
  • 11
  • 15
  • 1
    It should also be noted that `float` values are much more efficient with calculations versus `decimal.Decimal`. The latter should be used only as a last resort. 64-bit floats offer around ~16 decimal place precision, not arbitrary precision. – jpp Jul 10 '18 at 06:04
  • When in `decimal.Decimal`, the value is `string`, e.g. Decimal('30.00'). How do you read 30.00 only ? – Gathide Sep 23 '21 at 06:52
  • @jpp, `decimals` provide 'fast correctly-rounded decimal floating point arithmetic' see https://docs.python.org/3.6/library/decimal.html for a list of its advantages over `float`. – Gathide Sep 23 '21 at 06:58