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 exerciseMore exercises in Aggregating
- Total by group
- Group means as a flat table
- Count rows per group
- Named aggregations
- Group by two keys
- Pivot table with a fill value
- Counts with crosstab
- Shares with value_counts
- Several statistics at once
- Share of the group total with transform
- Find the row with the maximum
- Largest order per rep with idxmax
- Pivot table with totals
- Fill missing values with the group mean
- Distinct values per group with nunique
- Keep the groups above a threshold