首页 > 解决方案 > 查找数据框中不包含某些值的所有值

问题描述

假设我有一个带有城市和名称的熊猫数据框。当然每个城市都会有很多名字,比如:

Chicago   John
Chicago   Mary
Chicago   Jane

我意识到我有 1000 个不同的城市,但是当我按名称分组并计算有多少城市与约翰相关联时,我只看到 998。我如何找到其中没有任何“约翰”的城市?

标签: pythonpython-3.xpandaspandas-groupby

解决方案


你可以做

df.groupby('cities').filter(lambda x : (x['name']!='John').all())

推荐阅读