pandas: Top Products per Region

medium
Checkpoint challenge
Solve this to show you know the material and jump straight to Unit 4. Everything before it counts as cleared.

You have a sales DataFrame with columns region, product and revenue, where a product can appear in many rows.

Implement top_products(df, n=2) returning a DataFrame with columns region, product and revenue:

  • first total each product's revenue within each region;
  • keep the top n products per region by total revenue, breaking ties alphabetically by product;
  • sort the result by region, then by revenue from highest to lowest (then by product), with a fresh 0..k index.
#pandas#groupby#ranking2 visible + 2 hidden tests
solution.py · Python 3.13
Loading editor…
Run your code to see results. ⌘↵ to run · ⌘⇧↵ to submit
pandas: Top Products per Region · TechGradify