将大整数转换为pandas中的字符串(以避免科学计数法) [英] Converting long integers to strings in pandas (to avoid scientific notation)

查看:304
本文介绍了将大整数转换为pandas中的字符串(以避免科学计数法)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要使用

I want the following records (currently displaying as 3.200000e+18 but actually (hopefully) each a different long integer), created using pd.read_excel(), to be interpreted differently:

ipdb> self.after['class_parent_ref']
class_id
3200000000000515954    3.200000e+18
3200000000000515951             NaN
3200000000000515952             NaN
3200000000000515953             NaN
3200000000000515955    3.200000e+18
3200000000000515956    3.200000e+18
Name: class_parent_ref, dtype: float64

目前,它们似乎以科学标记的字符串出现":

Currently, they seem to 'come out' as scientifically notated strings:

ipdb> self.after['class_parent_ref'].iloc[0]
3.2000000000005161e+18

但是,更糟糕的是,我不清楚是否已经从我的.xlsx文件中正确读取了该数字:

Worse, though, it's not clear to me that the number has been read correctly from my .xlsx file:

ipdb> self.after['class_parent_ref'].iloc[0] -3.2e+18
516096.0

Excel中的数字(数据源)为3200000000000515952 .

这与显示无关,我知道我可以更改

This is not about the display, which I know I can change here. It's about keeping the underlying data in the same form it was in when read (so that if/when I write it back to Excel, it'll look the same and so that if I use the data, it'll look like it did in Excel and not Xe+Y). I would definitely accept a string if I could count on it being a string representation of the correct number.

您可能会注意到,我想要看到的数字实际上(偶然地)是标签之一.与我输入的数字不同,大熊猫正确地将它们读为字符串(也许是因为Excel将它们视为字符串?). (尽管实际上,即使我在重做读取之前在相关单元格中输入"3200000000000515952",我也会得到与上述相同的结果.)

You may notice that the number I want to see is in fact (incidentally) one of the labels. Pandas correctly read those in as strings (perhaps because Excel treated them as strings?) unlike this number which I entered. (Actually though, even when I enter ="3200000000000515952" into the cell in question before redoing the read, I get the same result described above.)

如何从数据框中获取3200000000000515952?我想知道熊猫是否对长整数有限制,但是我发现的唯一内容是1)有点过时了,2)看起来不像我要面对的一样.

How can I get 3200000000000515952 out of the dataframe? I'm wondering if pandas has a limitation with long integers, but the only thing I've found on it is 1) a little dated, and 2) doesn't look like the same thing I'm facing.

谢谢!

推荐答案

使用NaN将您的列值转换为0,然后将该列键入为整数以这样做.

Convert your column values with NaN into 0 then typcast that column as integer to do so.

df[['class_parent_ref']] = df[['class_parent_ref']].fillna(value = 0)
df['class_parent_ref'] = df['class_parent_ref'].astype(int)

或者在读取文件时,为pd.read_excel()指定keep_default_na = False,为pd.read_csv()指定na_filter = False

Or in reading your file, specify keep_default_na = False for pd.read_excel() and na_filter = False for pd.read_csv()

这篇关于将大整数转换为pandas中的字符串(以避免科学计数法)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆