ashunigion
ashunigion

Reputation: 1949

String of decimal values to numeric without round off

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

Upvotes: 0

Views: 1041

Answers (3)

ashunigion
ashunigion

Reputation: 1949

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

Upvotes: 1

mike.k
mike.k

Reputation: 3447

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

Upvotes: 1

Keveloper
Keveloper

Reputation: 783

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.

Upvotes: 1

Related Questions