Pandas - read_csv scientific notation large number

喜夏-厌秋 提交于 2020-08-05 16:43:36

问题


I am trying to read a csv file with pandas that has some rows in scientific notation.

When it reads the values it is not capturing the true underlying number. When I re-purpose the data the true value gets lost.

df = pd.read_csv('0_IDI_Submitter_out.csv')

The underlying true values that I am trying to preserve are as follows:

      INPUT: Extra 1
0     8921107
1     56300839420000
2     56207557000000

However, pandas reads it as

 INPUT: Extra 1
0     8921107
1     5.63008E+13
2     5.62076E+13

If I try to write a new csv or use this data the values show as:

 INPUT: Extra 1
0     8921107
1     56300800000000
2     56207600000000

How can I get pandas to read the true number rather than the scientific notation which causes it to convert incorrectly?


回答1:


Can't seem to reproduce your problem, but maybe this will work?

df = pd.read_csv('0_IDI_Submitter_out.csv', dtype={'INPUT: Extra 1':np.object_})

Also, check the dtypes of your dataframe:

result = df.dtypes
print(result)



回答2:


The problem appears to be that opening a CSV file in Excel which contains large numbers or strings that appear as large numbers like product codes, SKU's, UPC's etc are automatically converted into scientific notation. Once this has been done, you'll have to manually go into Excel and re-format but trying to do this from Pandas does not appear possible and the data integrity is lost.

However, if I never open the file in Excel and work on it purely through Pandas then it is all good. Similarly, if you work purely in Excel you're also good.

My ultimate conclusion is that when working with large numbers or strings that appear as large numbers like product codes or UPC's it is best not to mix pandas with Excel. As an alternative, I just started saving all my dataframes as pickle files instead of csv.

Hope that helps anyone in the future.

Thanks



来源:https://stackoverflow.com/questions/58067051/pandas-read-csv-scientific-notation-large-number

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!