I would like to filter a pandas DataFrame to rows where that particular row's group has a minimum count of a specific column value.
For example, return only the rows/groups of df where the ['c2','c3'] group has at least 2 rows with 'c1' value of 1:
df = pd.DataFrame({'c1':[0,1,0,1,1,0], 'c2':[0,0,0,1,1,1], 'c3':[0,0,0,1,1,1]})
The result should return only the rows with indices 3,4,5 since only the [c2,c3] = [1,1] group has at least 2 rows with 'c1' value of 1.
df.groupby(['c2','c3']).filter(lambda x: x['c1'].count() >= 2)
does not return the required result. I need the count to apply specifically to the count of 1s, not just any value of 'c1'.
The following works but I am not sure how to make it more pythonic:
s = df.groupby(['c2','c3']).apply(lambda x: x[x['c1']==1].count() >= 2).all(axis=1)
df = df.reset_index().set_index(['c2','c3']).loc[s[s].index].reset_index().set_index(['index'])