问题描述
我有一个数字列,其中可能包含另一个 [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 ?
推荐答案
我认为您想使用 pd.to_numeric(x,errors ='coerce')代替:
In [73]: x = pd.to_numeric(x, errors='coerce')
In [74]: x
Out[74]:
0 1.0
1 1.2
2 NaN
3 1.0
4 NaN
dtype: float64
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中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!