我有一个数字列,可以包含不同形式[0-9]的其他字符.说:x = pandas.Series(["1","1.2", "*", "1", "**."]).然后我想使用将该系列转换为数字列x.astype(dtype = float, errors = 'ignore').我无法弄清楚为什么熊猫不断给我一个错误,尽管我不让他这样做!我的代码有问题吗?
Max*_*axU 10
我想你想用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
Run Code Online (Sandbox Code Playgroud)
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', '**.']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5729 次 |
| 最近记录: |