Pandasdrill

Level 7: Interview cases Exercise 97 of 102

Top three customers and their share

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

The CEO asks who the three biggest customers are and what share of the business each one represents. Using completed orders, return the top three customers by revenue with the columns `customer`, `revenue` and `share`, where share is the customer's part of all completed revenue as a fraction. Largest first.

What this exercise teaches

The order of the steps decides the answer. The share must be computed against all revenue, so it comes before nlargest. Cutting to the top three first and then dividing makes their shares add up to 100%, which hides how much of the business comes from everyone else.

A hint if you get stuck

Work out every customer's share before you cut the table down to three rows.

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