2016-07-31 270 views
1

我怎样才能实现对过滤NOT条件大熊猫不能与过滤条件

grouped = store_ids_with_visits.groupby(level=[0, 1, 2]) 
grouped.filter(lambda x: (len(x) == 1 and x['template_fk'] == exterior_template)) 

我想不是的条件回答所有条目

我试图做:

grouped.filter(lambda x: ~(len(x) == 1 and x['template_fk'] == exterior_template)) 

但出现以下错误:

filter function returned a int, but expected a scalar bool 
+0

试着用'not'替换'〜'(按位NOT运算符) - 例如'not(len(x)...' – jedwards

+0

@jedwards用这个我得到'一个系列的真值不明确' –

+0

在你的数组中使用'any'或'all'内建函数 –

回答

1

IIUC,您可以使用isin检查布尔条件,只需要分组数据框的NOT(~)值:

df[~df.isin(grouped.filter(lambda x: (len(x) == 1 and x['template_fk'] == exterior_template)))]