解决方法:
@H_404_0@我想你可能会有更好的运气convert_objects
:
@H_404_0@
In [11]: s = pd.Series(['1', '2', 'a'])
In [12]: s.astype(int, raise_on_error=False) # just returns s
Out[12]:
0 1
1 2
2 a
dtype: object
In [13]: s.convert_objects(convert_numeric=True)
Out[13]:
0 1
1 2
2 NaN
dtype: float64
@H_404_0@更新:在最近的pandas中,不推荐使用convert_objects方法.赞成
pd.to_numeric
:
@H_404_0@
In [21]: pd.to_numeric(s, errors='coerce')
Out[21]:
0 1.0
1 2.0
2 NaN
dtype: float64
@H_404_0@这不像convert_objects那样强大/神奇(它也适用于DataFrames),但效果很好,在这种情况下更明确.阅读object conversion section of the docs,其中提到了其他to_ *函数.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。