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

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

type conversion in python from float to int

pythonpandas

提问by john

I am trying to change data_dfwhich is type float64to int.

我正在尝试将data_df类型更改float64int.

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_numericfirst because floatcannot 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 floatand 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_numericget error:

如果某些值无法转换并出现to_numeric错误:

ValueError: Unable to parse string

值错误:无法解析字符串

is possible add parameter errors='coerce'for convert non numeric to NaN.

可以添加参数errors='coerce'以将非数字转换为NaN.

If NaNvalues then cast to intis 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 NaNto some numeric e.g. 0use 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)