简体   繁体   English

在 Pandas (Python 3) 中用 0 替换 WhiteSpace

[英]Replace WhiteSpace with a 0 in Pandas (Python 3)

simple question here -- how do I replace all of the whitespaces in a column with a zero?这里有一个简单的问题——如何用零替换列中的所有空格?

For example:例如:

  Name       Age
  John       12
  Mary 
  Tim        15

into进入

  Name       Age
  John       12
  Mary       0
  Tim        15

I've been trying using something like this but I am unsure how Pandas actually reads whitespace:我一直在尝试使用这样的东西,但我不确定熊猫实际上是如何读取空格的:

 merged['Age'].replace(" ", 0).bfill()

Any ideas?有任何想法吗?

Use the built in method convert_objects and set param convert_numeric=True : 使用内置方法convert_objects并设置参数convert_numeric=True

In [12]:
# convert objects will handle multiple whitespace, this will convert them to NaN
# we then call fillna to convert those to 0
df.Age = df[['Age']].convert_objects(convert_numeric=True).fillna(0)
df
Out[12]:
   Name  Age
0  John   12
1  Mary    0
2   Tim   15

Here's an answer modified from this, more thorough question . 这是从这个更彻底的问题修改而来的答案。 I'll make it a little bit more Pythonic and resolve your basestring issue. 我将使用Pythonic,并解决您的basestring问题。

def ws_to_zero(maybe_ws):
    try:
        if maybe_ws.isspace():
            return 0
        else:
            return maybe_ws
    except AttributeError:
        return maybe_ws

d.applymap(ws_to_zero)

where d is your dataframe. d是您的数据框。

merged['Age'] = merged['Age'].apply(lambda x: 0 if x == ' ' else x)

if you want to use NumPy, then you can use the below snippet:如果你想使用 NumPy,那么你可以使用下面的代码片段:

import numpy as np    
df['column_of_interest'] = np.where(df['column_of_interest']==' ',0,df['column_of_interest']).astype(float)

While Paulo's response is excellent, my snippet above may be useful when multiple criteria are required during advanced data manipulation.虽然 Paulo 的回答非常好,但在高级数据操作过程中需要多个标准时,我上面的代码片段可能很有用。

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

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