我如何实现非条件过滤
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
最佳答案
IIUC,你可以使用isin
检查 bool 条件并仅采用分组数据帧的 NOT(~)
值:
df[~df.isin(grouped.filter(lambda x: (len(x) == 1 and x['template_fk'] == exterior_template)))]
关于python - Pandas 不具备过滤条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38681802/