pandas.DataFrame将所有字符串值设置为nan [英] pandas.DataFrame set all string values to nan

查看:1149
本文介绍了pandas.DataFrame将所有字符串值设置为nan的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个pandas.DataFrame,其中包含字符串,浮点和整数类型.

I have a pandas.DataFrame that contain string, float and int types.

是否可以将所有无法转换为float的字符串设置为NaN?

Is there a way to set all strings that cannot be converted to float to NaN ?

例如:

    A  B   C      D
0   1  2   5      7
1   0  4 NaN     15
2   4  8   9     10
3  11  5   8      0
4  11  5   8  "wajdi"

收件人:

    A  B   C      D
0   1  2   5      7
1   0  4 NaN     15
2   4  8   9     10
3  11  5   8      0
4  11  5   8    NaN

推荐答案

您可以使用pd.to_numeric并设置errors='coerce'

pandas.to_numeric

df['D'] = pd.to_numeric(df.D, errors='coerce')

哪个会给你:

    A   B   C   D
0   1   2   5.0 7.0
1   0   4   NaN 15.0
2   4   8   9.0 10.0
3   11  5   8.0 0.0
4   11  5   8.0 NaN

不建议使用的解决方案(仅熊猫< = 0.20):

Deprecated solution (pandas <= 0.20 only):

df.convert_objects(convert_numeric=True)

pandas.DataFrame.convert_objects

这是convert_objects源代码# TODO: Remove in 0.18 or 2017, which ever is sooner中的开发人员注释.因此,如果使用它,请勿将其作为长期解决方案.

Here's the dev note in the convert_objects source code: # TODO: Remove in 0.18 or 2017, which ever is sooner. So don't make this a long term solution if you use it.

这篇关于pandas.DataFrame将所有字符串值设置为nan的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆