简体   繁体   English

pandas.DataFrame将所有字符串值设置为nan

[英]pandas.DataFrame set all string values to nan

I have a pandas.DataFrame that contain string, float and int types. 我有一个包含string,float和int类型的pandas.DataFrame

Is there a way to set all strings that cannot be converted to float to NaN ? 有没有办法将所有无法转换为浮点数的字符串设置为NaN

For example: 例如:

    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"

to: 至:

    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

You can use pd.to_numeric and set errors='coerce' 你可以使用pd.to_numeric并设置errors='coerce'

pandas.to_numeric pandas.to_numeric

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

Which will give you: 哪个会给你:

    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

Deprecated solution (pandas <= 0.20 only): 不推荐使用的解决方案 (仅适用于pandas <= 0.20):

df.convert_objects(convert_numeric=True)

pandas.DataFrame.convert_objects pandas.DataFrame.convert_objects

Here's the dev note in the convert_objects source code: # TODO: Remove in 0.18 or 2017, which ever is sooner . 这是convert_objects源代码中的开发注释: # TODO: Remove in 0.18 or 2017, which ever is sooner So don't make this a long term solution if you use it. 因此,如果您使用它,请不要将其作为长期解决方案。

Here is a way: 这是一种方式:

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

And then you have: 然后你有:


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

You can use pd.to_numeric with errors='coerce' . 您可以使用带errors='coerce' pd.to_numeric

In [30]: df = pd.DataFrame({'a': [1, 2, 'NaN', 'bob', 3.2]})

In [31]: pd.to_numeric(df.a, errors='coerce')
Out[31]: 
0    1.0
1    2.0
2    NaN
3    NaN
4    3.2
Name: a, dtype: float64

Here is one way to apply it to all columns: 以下是将其应用于所有列的一种方法:

for c in df.columns:
    df[c] = pd.to_numeric(df[c], errors='coerce')

(See comment by NinjaPuppy for a better way.) (请参阅NinjaPuppy的评论以获得更好的方法。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM