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 exerciseMore exercises in Analysis
- Rolling average
- Month over month growth
- Running total
- Rank with ties
- Change from the previous period
- Monthly totals with resample
- Quartile bins with qcut
- Age bands with cut
- Classify with apply
- Running total per group
- Top two orders per region
- Monthly totals with to_period
- Days since the previous order
- Dense rank within a group
- Label rows with np.where
- Drawdown from the running high