Python pandas dataframe group by based on a condition Python pandas dataframe group by based on a condition pandas pandas

Python pandas dataframe group by based on a condition


Try this code:

df.groupby('column').filter(lambda group: group.size > X)


The grouped result is a regular DataFrame, so just filter the results as usual:

 import pandas as pd df = pd.DataFrame({'a': ['a', 'b', 'a', 'a', 'b', 'c', 'd']}) after = df.groupby('a').size() >> after a a    3 b    2 c    1 d    1 dtype: int64 >> after[after > 2] a a    3 dtype: int64