简体   繁体   English

在python中将类型从float转换为int

[英]type conversion in python from float to int

I am trying to change data_df which is type float64 to int . 我正在尝试将类型float64 data_df更改为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”

I think you need to_numeric first because float cannot be cast to int : 我认为您首先需要to_numeric因为无法将floatint

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 ValueError:无法解析字符串

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请参阅docs

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 eg 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

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. 有效的方法是data_df['grade'] = int(pd.to_numeric(data_df['grade']))方法as_type(int)抛出并出错,因为它想告诉您,从浮点数到整数的精确转换是不可能的您将丢失信息。 My solution will truncate the integer (ie 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 (ie 1.9 will become 2) 我的解决方案将截断整数(即1.9将变为1),因此您可能想在问题中指定是否要通过截断或舍入将float转换为整数(即​​1.9将变为2)

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')

我发现这对我有用,其他先前的答案都对我没有帮助:

data_df['grade'] = data_df['grade'].apply(np.int)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM