Pandasdrill

Level 3: Aggregating Exercise 35 of 63 Pro

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 exercise

More exercises in Aggregating