pandas Int 太大而无法在执行 .astype(int) 时转换为 C long

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49328235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 05:20:30  来源:igfitidea点击:

Int too large to convert to C long while doing .astype(int)

pythonpandasnumpy

提问by Lucas Pelin

I'm running a python script, where I have to convert a string column from a pandas dfto int, using the astype(int)method. However, I get this following error:

我运行一个python脚本,在那里我有一个字符串列从Pandas转换dfint使用astype(int)方法。但是,我收到以下错误:

"Python int too large to convert to C long"

My strings are all numbers in string format, up to 15 characters. Is there a way to convert this column to int type without this popping this error?

我的字符串都是字符串格式的数字,最多15个字符。有没有办法将此列转换为 int 类型而不会弹出此错误?

采纳答案by ALollz

You need to use .astype('int64')

你需要使用 .astype('int64')

import pandas as pd
df = pd.DataFrame({'test': ['999999999999999','111111111111111']})
df['int'] = df['test'].astype('int64')

回答by Sandeep Bomma

Check whether you are assigning a Large Integer values to variable.

检查您是否将大整数值分配给变量。

1) Instead of applying multiplication, using power

1) 不使用乘法,而是使用幂

For example:

例如:

x = [10**i for i in range(1,1000)] instead of [10*i for i in range(1,1000)]  ,which will cross the variable limit