Pandasdrill

Level 3: Aggregating Exercise 46 of 102

Rows from large groups with filter

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

Return every order placed by a rep who has at least three orders in `sales`. Keep all columns, the original row order and the original row labels.

What this exercise teaches

filter on a groupby returns the original rows, not one row per group: every row of a group that passes the test stays, in its original position. transform("count") gives the same answer as a mask, by writing each group's size next to every row.

A hint if you get stuck

groupby(...).filter(function) keeps or drops whole groups: the function gets one group at a time and returns True to keep all of its rows.

Try it now, free, no account needed

Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.

Open this exercise

More exercises in Aggregating