python - Handling HUGE numbers in numpy or pandas -
i doing competition provided data anonymized. quite few of columns have huge values. largest 40 digits long! used pd.read_csv
columns have been converted objects result.
my original plan scale data down since seen objects can't arithmetic on these.
does have suggestion on how handle huge numbers in pandas or numpy?
note i've tried converting value uint64
no luck. error "long big convert"
you can use pandas converters call int
or other custom converter function on string being imported:
import pandas pd stringio import stringio txt='''\ line,big_num,text 1,1234567890123456789012345678901234567890,"that sure big number" 2,9999999999999999999999999999999999999999,"that bigger number" 3,1,"tiny" 4,-9999999999999999999999999999999999999999,"really negative" ''' df=pd.read_csv(stringio(txt), converters={'big_num':int}) print df
prints:
line big_num text 0 1 1234567890123456789012345678901234567890 sure big number 1 2 9999999999999999999999999999999999999999 bigger number 2 3 1 tiny 3 4 -9999999999999999999999999999999999999999 negative
now test arithmetic:
n=df["big_num"][1] print n,n+1
prints:
9999999999999999999999999999999999999999 10000000000000000000000000000000000000000
if have values in column might cause int
croak, can this:
txt='''\ line,big_num,text 1,1234567890123456789012345678901234567890,"that sure big number" 2,9999999999999999999999999999999999999999,"that bigger number" 3,0.000000000000000001,"tiny" 4,"a string","use 0 strings" ''' def conv(s): try: return int(s) except valueerror: try: return float(s) except valueerror: return 0 df=pd.read_csv(stringio(txt), converters={'big_num':conv}) print df
prints:
line big_num text 0 1 1234567890123456789012345678901234567890 sure big number 1 2 9999999999999999999999999999999999999999 bigger number 2 3 1e-18 tiny 3 4 0 use 0 strings
then every value in column either python int or float , support arithmetic.
Comments
Post a Comment