对于无法使用 astype 进行转换的值使用 NaN
我有一个非常大的 Pandas DataFrame,如下所示:
>>> d = pd.DataFrame({"a": ["1", "U", "3.4"]})
>>> d
a
0 1
1 U
2 3.4
目前该列被设置为 object
:
>>> d.dtypes
a object
dtype: object
我想将此列转换为 float,以便我可以使用 groupby()< /code> 并计算平均值。当我使用
astype
尝试它时,我正确地收到错误,因为字符串无法转换为浮动:
>>> d.a.astype(float)
ValueError: could not convert string to float: 'U'
我想做的是将所有元素转换为浮动,然后替换那些不能被 NaN 转换的。
我该怎么做?
我尝试设置 raise_on_error
,但它不起作用,dtype
仍然是 object
。
>>> d.a.astype(float, raise_on_error=False)
0 1
1 U
2 3.4
Name: a, dtype: object
I have a very large Pandas DataFrame that looks like this:
>>> d = pd.DataFrame({"a": ["1", "U", "3.4"]})
>>> d
a
0 1
1 U
2 3.4
Currently the column is set as an object
:
>>> d.dtypes
a object
dtype: object
I'd like to convert this column to float so that I can use groupby()
and compute the mean. When I try it using astype
I correctly get an error because of the string that can't be cast to float:
>>> d.a.astype(float)
ValueError: could not convert string to float: 'U'
What I'd like to do is to cast all the elements to float, and then replace the ones that can't be cast by NaNs.
How can I do this?
I tried setting raise_on_error
, but it doesn't work, the dtype
is still object
.
>>> d.a.astype(float, raise_on_error=False)
0 1
1 U
2 3.4
Name: a, dtype: object
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
to_numeric
并指定errors='coerce'
以强制无法解析为数值的字符串变为NaN
:Use
to_numeric
and specifyerrors='coerce'
to force strings that can't be parsed to a numeric value to becomeNaN
: