当我使用pandas.DataFrame.replace(dict)
时转换user_id string
至integer
,我收到:
"OverflowError: Python int too large to convert to C long".
示例代码:
import pandas as pd
x = {'user_id':['100000715097692381911',
'100003840837471130074'],
'item_id': [1, 2]
}
dfx = pd.DataFrame(x)
dfx['user_id'].replace(
{
'100000715097692381911': 0,
'100003840837471130074': 1
}, inplace=True)
我不明白为什么这是重复的。我认为这是 pandas 将 str 类型作为整数的问题。我没有将这些大 id 数字作为整数加载,而是作为字符串加载。好吧,如果我在“user_id”字符串前面添加一个字符,例如“s100000715097692381911”,它就不会报告 OverflowError。
最佳答案
在 C 中,long 为 4 个字节,只能存储 -2,147,483,648
和 2,147,483,647
之间的值。
为了回答你的另一个问题,C 中的字符串存储为字符数组,因此每个字符的内存空间为 1 个字节,加上终止指针的大小。这意味着 C 中的 python 字符串不会导致溢出,但大整数会导致溢出。
来源:https://www.tutorialspoint.com/cprogramming/c_data_types.htm
关于python - pandas.DataFrame.replace() 得到 "OverflowError: Python int too large to convert to C long",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55065170/