Matthew Soh
Matthew Soh

Reputation: 133

Pandas read JSON causes values to convert into scientific notation

I am working with a pandas Dataframe in which one column contains a 27 digit number. I converted the Dataframe into a JSON array using:

pd.read_json(df, orient='split')

When converting the JSON array back into a Dataframe, I used:

 pd.read_json(df, orient='split')

However, in the conversion, the values were converted to a scientific notation.

Actual: 8.123456e+27
Expected: 8123456342700123300640123456

Is there a way I can get around this? Apologies for only providing a sudo code, I'm working with confidential information.

Upvotes: 1

Views: 3066

Answers (2)

Matthew Soh
Matthew Soh

Reputation: 133

I managed to find a work around by including an additional parameter within pd.read_json

pd.read_json(df, orient='split', dtype = {"column_name": object})

I used this as reference

Thank you for all the help!

Upvotes: 5

Shivam Singh
Shivam Singh

Reputation: 1624

You can use

pd.set_option('display.float_format', lambda x: '%d' % x)

For example, with this sample data

df = pd.DataFrame([['8123456342700123300640123456']],
                   index=['row'],
                   columns=['col'])
print(df.dtypes)
dx = df.to_json(orient='split')
dg = pd.read_json(dx, orient='split')
print(dg)
print(dg.dtypes)

Output: (Notice float64 for col)

col    object
dtype: object
                             col
row 8123456342700123332831870976
col    float64
dtype: object

To change it back to object, you can use

dg = pd.read_json(dx, orient='split').astype(object)
print(dg)
print(dg.dtypes)

Output:

                             col
row 8123456342700123332831870976
col    object
dtype: object

Upvotes: 0

Related Questions