Pandasdrill

Level 5: Analysis Exercise 78 of 102

First order per customer

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 each customer's earliest order as a DataFrame with the columns `cust`, `date` and `amount`, one row per customer, sorted by `cust` A to Z. The rows of `orders` are not in date order. Row labels do not matter.

What this exercise teaches

Sorting and then dropping duplicates keeps a whole row per customer, so the amount belongs to the earliest order. Taking min() of date and amount per group looks similar but takes each minimum on its own: the smallest amount usually comes from a different order than the earliest date.

A hint if you get stuck

Sort by date first. drop_duplicates on the customer column then keeps the first row it meets for each customer, which is the earliest order.

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 Analysis