Largest order per rep with idxmax
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
For each rep, return their single highest-revenue order as a DataFrame with the columns `rep`, `order_id` and `revenue`, one row per rep sorted by rep A to Z. Row labels do not matter.
What this exercise teaches
idxmax per group returns the label of the top row in each group, sorted by group key, and loc pulls those whole rows so order_id and revenue stay together. Taking max() of both columns separately would pair each rep's highest order_id with their highest revenue, which usually come from different orders.
A hint if you get stuck
groupby("rep")["revenue"].idxmax() gives one row label per rep. Pass those labels to loc.
Try it now, free, no account needed
The first 15 tasks 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 adds timed mode and the certificate.
Open this exerciseMore exercises in Aggregating
- Total by groupPro
- Group means as a flat tablePro
- Count rows per groupPro
- Named aggregationsPro
- Group by two keysPro
- Pivot table with a fill valuePro
- Counts with crosstabPro
- Shares with value_countsPro
- Several statistics at oncePro
- Share of the group total with transformPro
- Pivot table with totalsPro
- Fill missing values with the group meanPro