Skip to content Skip to sidebar Skip to footer

Converting Exponential Notation Numbers To Strings - Explanation

I have DataFrame from this question: temp=u'''Total,Price,test_num 0,71.7,2.04256e+14 1,39.5,2.04254e+14 2,82.2,2.04188e+14 3,42.9,2.04171e+14''' df = pd.read_csv(pd.compat.StringI

Solution 1:

When you use pd.read_csv to import data and do not define datatypes, pandas makes an educated guess and in this case decides, that column values like "2.04256e+14" are best represented by a float value.

This, converted back to string adds a ".0". As you corrently write, converting to int64 fixes this.

If you know that the column has int64 values only before input (and no empty values, which np.int64 cannot handle), you can force this type on import to avoid the unneeded conversions.

import numpy as np

temp=u"""Total,Price,test_num
0,71.7,2.04256e+14
1,39.5,2.04254e+14
2,82.2,2.04188e+14
3,42.9,2.04171e+14"""

df = pd.read_csv(pd.compat.StringIO(temp), dtype={2: np.int64})

print(df)

returns

   Total  Price         test_num
0      0   71.7  204256000000000
1      1   39.5  204254000000000
2      2   82.2  204188000000000
3      3   42.9  204171000000000

Post a Comment for "Converting Exponential Notation Numbers To Strings - Explanation"