使用函数时,将浮点转换为int不起作用



我在数据帧中有一列需要加入。该列包含混合数据类型,例如:

s = pd.Series([3985500,'3985500',3985500.0,'3985500.0','3985500A','3985500B'])

我正在尝试将所有数值转换为int,以确保在连接时找到键。无论字符串是什么,都可以保留为字符串,并且最终的列格式可以是字符串,只要浮点转换为int即可。

我尝试过astype(),但它忽略了浮点,出于某种原因,我一直在使用科学符号(见索引2和3(:

s.astype(int, errors='ignore')
0       3985500
1       3985500
2    3.9855e+06
3     3985500.0
4      3985500A
5      3985500B
dtype: object

我让pd.to_numerictry-except:处理浮点

try: int(pd.to_numeric(s[3]))
except ValueError: s[3]
3985500
dtype: int

然而,当我在函数中尝试它时,它什么也不返回:

def convert_to_int(cell):
try: int(pd.to_numeric(cell))
except ValueError: cell
convert_to_int(s[3])

知道为什么会发生这种事吗?可能还有其他解决方法,但为什么它在函数中不起作用?

我希望将此函数与s.apply()一起使用。我看过几个类似的帖子:

  • 检查字符串是否可以在Python中转换为浮点
  • Python如何将Series类型:object转换为int

您没有从函数(即(返回任何值

def convert_to_int(cell):
try: 
return int(pd.to_numeric(cell))
except ValueError: 
cell
convert_to_int(s[3])

和使用apply((语法:

s.apply(lambda x:convert_to_int(x))

您需要将所有数字类型的值转换为int。您不需要有单独的功能,只需使用Series.apply,如下所示:

In [202]: s = pd.Series([3985500,'3985500',3985500.0,'3985500.0','3985500A','3985500B'])
In [203]: s = s.apply(lambda x:x if isinstance(x, str) else int(x)) 
In [204]: s
Out[204]: 
0      3985500
1      3985500
2      3985500
3    3985500.0
4     3985500A
5     3985500B
dtype: object

上述命令将所有数字类型转换为int,保持string不变。

最新更新