Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Filtering Pandas dataframe Group by count value

I’m trying to filter the group by count from a pandas dataframe so I only end up with values over a certain amount.

Example dataframe:

Name Thing Count
Fred Apple 1
Harry Banana 1
Sonia Banana 1
Pete Apple 1
Tracy Apple 1
df.groupby(["Thing"]).sum()['Count']

Current output:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thing
Apple 3
Banana 2

But my desired output is to only include a Thing if the sum value is more than 2:

Thing
Apple 3

I can only find examples of filtering df columns when using groupby. How do you filter based on the aggregated values?

>Solution :

Just filter it

out = df.groupby(["Thing"]).sum()['Count'].loc[lambda x : x>2]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading