pandas 忽略熊猫 astype 中的错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50858746/
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
Ignore errors in pandas astype
提问by Neroksi
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 ?
我有一个数字列,它可以包含另一个不同形式的字符[0-9]。说:x = pandas.Series(["1","1.2", "*", "1", "**."])
。然后,我要转换的是意甲成使用数值列x.astype(dtype = float, errors = 'ignore')
。我就是不明白为什么 Pandas 总是给我一个错误,尽管我要求他不要!我的代码有问题吗?
回答by MaxU
I think you want to use pd.to_numeric(x, errors='coerce')instead:
我想你想用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 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:
PS实际上x.astype(dtype = float, errors = 'ignore')
- 按预期工作,它不会出错,它只是保留系列,因为它无法转换某些元素:
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', '**.']