Keep the groups above a threshold
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 the total `revenue` per region as a Series indexed by region, but only for the regions whose total is above 600.
What this exercise teaches
The condition is about the group total, so it can only be applied after the groupby. Filtering the rows first answers a different question: which single orders are above 600. In SQL terms this is HAVING, not WHERE.
A hint if you get stuck
Aggregate first, then filter the result: the totals are a Series, and a boolean mask on that Series keeps the regions you want.
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
- Rows from large groups with filter