Revenue by category in pandas

mediumPipelines~20 min · 6 tests · bitshop

The bitshop data lives in three tables: orders, order_items and products. Revenue sits on the line items, the category sits on the product, and the status sits on the order. You need all three.

Write revenue_by_category(orders, items, products). Each argument is a pandas DataFrame loaded from the matching bitshop CSV:

  • orders: order_id, customer_id, order_date, status
  • items: order_id, product_id, quantity, unit_price
  • products: product_id, name, category, price

Return a DataFrame with exactly two columns, category and revenue:

  • revenue is the sum of quantity * unit_price over line items whose order is not cancelled. Returned orders still count.
  • Use unit_price from the line item, not the product’s list price.
  • One row per category that has revenue. Leave out categories with no qualifying items.
  • Sort by revenue descending, then category ascending. Don’t round.
>>> revenue_by_category(orders, items, products).head(2)
   category    revenue
0  Hardware  157421.34
1   Courses   58614.63
Learn the ideaRead the lesson →
solution.pyPython 3.14 · Pyodide

Press ⌘ ↵ or Run to check the visible tests. Submit runs the hidden ones too. Everything runs in your browser.

⌘ ↵ to run