pandas 将对象转换为大型数据帧的 int
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/49166701/
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:17:16 来源:igfitidea点击:
converting object to int of a large dataframe
提问by pylearner
how can I convert a large dataframe of a column from object to int.
如何将一列的大数据框从 object 转换为 int。
Dataframe :
数据框:
user
1101110110100
1111222555555
1112223365556
1113656560005
asaseee"
tdyhhdy"
dtype: object
expected:
预期的:
user
1101110110100
1111222555555
1112223365556
1113656560005
dtype: int64
I have used the below codes.
我使用了以下代码。
df['user'].astype(int)
df['user'].astype(str).astpe(int)
回答by jezrael
Try cast to int64
:
尝试投射到int64
:
df['user'] = df['user'].astype(np.int64)
Or:
或者:
df['user'] = df['user'].astype('int64')
print (df['user'])
0 1101110110100
1 1111222555555
2 1112223365556
3 1113656560005
Name: user, dtype: int64
EDIT:
编辑:
#convert not parseable values to NaNs
df['user'] = pd.to_numeric(df['user'], errors='coerce')
#remove rows with NaNs
df = df.dropna('user')
df['user'] = df['user'].astype(np.int64)
Or:
或者:
df['user'] = pd.to_numeric(df['user'], errors='coerce').fillna(0)
df['user'] = df['user'].astype(np.int64)
回答by Eliethesaiyan
you can cast into big int using numpy
您可以使用 numpy 转换为 big int
import numpy as np
df['user'] = df['user'].astype(np.int64)