pandas python中从float到int的类型转换
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40707158/
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
type conversion in python from float to int
提问by john
I am trying to change data_df
which is type float64
to int
.
我正在尝试将data_df
类型更改float64
为int
.
data_df['grade'] = data_df['grade'].astype(int)
I get the following error.
我收到以下错误。
invalid literal for int() with base 10: '17.44'
基数为 10 的 int() 的无效文字:'17.44'
回答by jezrael
I think you need to_numeric
first because float
cannot be cast to int
:
我认为您to_numeric
首先需要,因为float
不能强制转换为int
:
data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int)
Another solution is first cast to float
and then to int
:
另一种解决方案是先转换为float
然后转换为int
:
data_df['grade'] = data_df['grade'].astype(float).astype(int)
Sample:
样本:
data_df = pd.DataFrame({'grade':['10','20','17.44']})
print (data_df)
grade
0 10
1 20
2 17.44
data_df['grade'] = pd.to_numeric(data_df['grade']).astype(int)
print (data_df)
grade
0 10
1 20
2 17
data_df['grade'] = data_df['grade'].astype(float).astype(int)
print (data_df)
grade
0 10
1 20
2 17
---
---
If some values cannot be converted and after to_numeric
get error:
如果某些值无法转换并出现to_numeric
错误:
ValueError: Unable to parse string
值错误:无法解析字符串
is possible add parameter errors='coerce'
for convert non numeric to NaN
.
可以添加参数errors='coerce'
以将非数字转换为NaN
.
If NaN
values then cast to int
is not possible see docs:
如果NaN
值然后转换int
为不可能看到文档:
data_df = pd.DataFrame({'grade':['10','20','17.44', 'aa']})
print (data_df)
grade
0 10
1 20
2 17.44
3 aa
data_df['grade'] = pd.to_numeric(data_df['grade'], errors='coerce')
print (data_df)
grade
0 10.00
1 20.00
2 17.44
3 NaN
If want change NaN
to some numeric e.g. 0
use fillna
:
如果想更改NaN
为一些数字,例如0
使用fillna
:
data_df['grade'] = pd.to_numeric(data_df['grade'], errors='coerce')
.fillna(0)
.astype(int)
print (data_df)
grade
0 10
1 20
2 17
3 0
Small advice:
小建议:
Before using errors='coerce'
check all rows where is impossible casting to numeric by boolean indexing
:
在使用之前errors='coerce'
检查所有不可能转换为数字的行boolean indexing
:
print (data_df[pd.to_numeric(data_df['grade'], errors='coerce').isnull()])
grade
3 aa
回答by zabeltech
what works is data_df['grade'] = int(pd.to_numeric(data_df['grade']))
The method as_type(int)
throws and error because it want's to tell you, that no exact conversion from float to integer is possible and you will lose information.
My solution will truncate the integer (i.e. 1.9 will become 1), so you might want to specifiy in your question wether you want to convert float to integer by truncation or by rounding (i.e. 1.9 will become 2)
有效的是data_df['grade'] = int(pd.to_numeric(data_df['grade']))
该方法as_type(int)
抛出并出错,因为它想告诉您,从浮点数到整数的精确转换是不可能的,您将丢失信息。我的解决方案将截断整数(即 1.9 将变为 1),因此您可能想在问题中指定是要通过截断或舍入将浮点数转换为整数(即 1.9 将变为 2)
回答by Ruperto
From:
从:
data_df['grade'] = data_df['grade'].astype(int)
Need to change int into 'int'
需要将 int 更改为 'int'
data_df['grade'] = data_df['grade'].astype('int')
回答by befpy
I found this to work for me where none of the other earlier answers did the job for me:
我发现这对我有用,而其他早期的答案都没有对我来说有效:
data_df['grade'] = data_df['grade'].apply(np.int)
data_df['grade'] = data_df['grade'].apply(np.int)