忽略 pandas astype中的错误 [英] Ignore errors in pandas astype

查看:190
本文介绍了忽略 pandas astype中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数字列,其中可能包含另一个 [0-9] 格式的字符.说:x = pandas.Series(["1","1.2", "*", "1", "**."]). 然后,我想使用x.astype(dtype = float, errors = 'ignore')将该serie转换为数字列.尽管我要求他不要这样做,但我只是想不通为什么熊猫会一直给我一个错误!我的代码有问题吗?

I have a numeric column that could contain another characters different form [0-9]. Say: x = pandas.Series(["1","1.2", "*", "1", "**."]). Then I want to convert that serie into a numerical column using x.astype(dtype = float, errors = 'ignore') . I just can't figure out why Pandas keeps giving me an error despite the fact that I ask him not to! Is there something wrong with my code ?

推荐答案

我认为您想使用 PS实际上x.astype(dtype = float, errors = 'ignore')-可以正常工作,它不会产生错误,它只能保留序列,因为它不能转换某些元素:

PS actually x.astype(dtype = float, errors = 'ignore') - works as expected, it doesn't give an error, it just leaves series as it is as it can't convert some elements:

In [77]: x.astype(dtype = float, errors = 'ignore')
Out[77]:
0      1
1    1.2
2      *
3      1
4    **.
dtype: object   # <----- NOTE!!!

In [81]: x.astype(dtype = float, errors = 'ignore').tolist()
Out[81]: ['1', '1.2', '*', '1', '**.']

这篇关于忽略 pandas astype中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆