如何在python中找到大小超过值的组?

时间:2016-06-15 14:50:52

标签: python pandas

我将数据读入DataFrame并将其称为数据。我在python中有以下查询:

data[data["gender"]=="male"].groupby('age').city.nunique().sort_values(ascending=False)


age
29    86
24    85
21    81
25    81
20    81
28    78
27    78

现在我想找到那些大小超过80的组。我怎么能在python中做到这一点?

1 个答案:

答案 0 :(得分:0)

汇总和排序调用的结果是一个pandas系列,其索引是您要查找的组。因此,要查找具有大于某个cutOffvalue

的组
cutOffValue = 80
counts = data[data["gender"]=="male"].groupby('age').city.nunique().sort_values(ascending=False)
groups = counts[counts > cutOffValue].index

当然,如果您想将它作为列表或集合,您可以轻松地转换最终值

groups = list(groups)