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.