python - pandas.DataFrame.replace() 得到 "OverflowError: Python int too large to convert to C long"

标签 python python-3.x pandas dataframe

当我使用pandas.DataFrame.replace(dict)时转换user_id stringinteger ,我收到:

"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,6482,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/

相关文章:

python - 你什么时候在 Python 中使用 'self'?

python - 防止eventlet中的greenthread切换

python - 列表实际上如何使用for循环在python中工作?

调用 `.info()` 时,带有列表元素的 Python Pandas Dataframe 报告错误类型?

pandas - 如何通过分组来填充 NaN?

python - 查找具有非 na 值的列,并使用非 na 列的名称创建第三列填充值

python - 如何将文本更改为数字并在 Python 中保存到数组中

python - CSV 以字节形式返回而不是字符串 Python

python - pandas 中任意分布的频率均值计算

python - 回调查询 Telegram 游戏