Pandas将匹配条件的单元格替换为None

时间:2018-02-01 19:17:38

标签: python pandas

采用以下示例数据框:

df = pd.DataFrame([['de', None, None], 
                   ['de ditos', 2, 3], 
                   [4, None, None], 
                   [None, None, 9],
                   ['de', 4, 6]])

看起来像

          0    1    2
0        de  NaN  NaN
1  de ditos  2.0  3.0
2         4  NaN  NaN
3      None  NaN  9.0
4        de  4.0  6.0

我想要替换第0列中的所有值,其中单元格值是' de'如果没有,那么数据框的结尾如下:

          0    1    2
0      None  NaN  NaN
1  de ditos  2.0  3.0
2         4  NaN  NaN
3      None  NaN  9.0
4      None  4.0  6.0

我试过了:

df[0].where(df[0] == 'de') = None

返回SyntaxError:不能分配给函数调用

我也尝试过:

def erasedes(x):
    if x == 'de':
        return None
    else: pass
df[0] = df[0].apply(lambda x: erasedes(x))

但是这会用None

替换每个值

2 个答案:

答案 0 :(得分:2)

你真正想要的是:

In [3]: df
Out[3]:
          0    1    2
0        de  NaN  NaN
1  de ditos  2.0  3.0
2         4  NaN  NaN
3      None  NaN  9.0
4        de  4.0  6.0

In [4]: df.loc[df[0] == 'de',0] = None

In [5]: df
Out[5]:
          0    1    2
0      None  NaN  NaN
1  de ditos  2.0  3.0
2         4  NaN  NaN
3      None  NaN  9.0
4      None  4.0  6.0

注意,您的.apply方法不起作用,因为您的erasedes函数始终返回Noneelse:pass什么都不做,函数终止,并隐式返回None。如果您使用else: return x

,它会起作用
In [6]: df = pd.DataFrame([['de', None, None],
   ...:                    ['de ditos', 2, 3],
   ...:                    [4, None, None],
   ...:                    [None, None, 9],
   ...:                    ['de', 4, 6]])

In [7]: def erasedes(x):
   ...:     if x == 'de':
   ...:         return None
   ...:     else:
   ...:         return x
   ...:
In [8]: df[0]
Out[8]:
0          de
1    de ditos
2           4
3        None
4          de
Name: 0, dtype: object

In [9]: df[0].apply(erasedes)
Out[9]:
0        None
1    de ditos
2           4
3        None
4        None
Name: 0, dtype: object

您应该选择基于.loc / .iloc的{​​{1}}作业,而.apply通常会很慢。

答案 1 :(得分:1)

这应该有效:

df[0]= df[0].replace({'de': None})