Pandasdrill

Level 7: Interview cases Exercise 93 of 102

Average order value by segment

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

Pricing wants the average value of a completed order in each customer segment, and how many completed orders it is based on. Return the columns `segment`, `orders` and `avg_order`, and leave out segments with fewer than two completed orders.

What this exercise teaches

The average has to be taken over orders, not over customers. Averaging each customer first and then averaging those numbers gives a customer with one order the same weight as a customer with ten, and the result is a different figure.

A hint if you get stuck

Named aggregations let you compute the count and the mean in one agg call, each with its own column name.

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 Interview cases