Alex Alex - 1 year ago 43
Python Question

How can I find the groups with size more than a value in python?

I read the data into a DataFrame and called it data. I have the following query in python:


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

now I want to find those groups whose size is more than 80. how can I do that in python?

Answer Source

The result of your aggregation and sorting call is a pandas series whose index are the groups you are looking for. So to find the groups with greater than a certain cutOffvalue

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

And of course, if you want it as a list or set, you could easily cast the final value

groups = list(groups)