Dealing with decimals with many digits in Pandas,

Viewed 153
pd.set_option('display.max_colwidth', None )
pd.set_option('display.float_format', lambda x: '%.200f' % x)


exData = pd.read_csv('AP11.csv',delimiter=';',float_precision=None)
    
x = exData.loc[:,['A','B']]
y = exData.loc[:,['C']]
x

my original float on excel is 0.1211101931541032183754113717355410323332315436353654273243543132542237415430173719

what is being displayed is 0.12111019315410319341363987177828676067292690277099609375000000000000000000000000000000000000000000000000000000000...

this is not a display issue. something in pandas rounds my float. i don't want to round any number for it will affect the result of my string. because this is originally a string that is converted to a float. i tried to use int64 but it can't handle big numbers. so instead i decided to use floats with "0.mystring" to not get "inf" displayed in pandas. and i get it rounded. is machine learning limited by these missy variables? or is there another way to deal with big numbers without rounding, displaying inf?

1 Answers

Use decimal instead of float. Just put

from decimal import Decimal

at the top of your code, and write your floats as

x = Decimal(0.121110193154103218375411371735541032333231543635365427324354313254223741543017371)

decimal is a library for floats with a dynamic length, rather than rounded.
Generally you should avoid floats, as they can have strange irregularities and roundings. Often when operations are performed on them, they can have a series of zeros and then some other numbers, when it should just have a few decimal places.

Related