Top Three Sellers per Warehouse
Problem
A retailer ranks products by units sold within each warehouse.
DataFrame: `products` (from `products.csv`)
| Column | Type |
|---------------|---------|
| product_id | int |
| product_name | object |
| units_sold | int |
| warehouse_id | int |
DataFrame: `warehouses` (from `warehouses.csv`)
| Column | Type |
|----------------|--------|
| warehouse_id | int |
| warehouse_name | object |
A product is a **best seller** in its warehouse if its `units_sold` is among the **top three distinct** unit-sold values for that warehouse. Several products may share a unit-sold value, and a tied value counts as one of the three distinct levels (use a dense rank).
Write a function that returns the best sellers as three columns named `warehouse`, `product`, and `units`, in any order.
Input data
Example rows — the live problem includes the full dataset.
| product_id | product_name | units_sold | warehouse_id |
|---|---|---|---|
| 1 | Stapler | 85 | 1 |
| 2 | Lamp | 80 | 2 |
| 3 | Mug | 60 | 2 |
| 4 | Chair | 90 | 1 |
| 5 | Pen Set | 69 | 1 |
| warehouse_id | warehouse_name |
|---|---|
| 1 | North |
| 2 | South |
Expected output
Your answer should return 6 rows with the columns warehouse, product, units.
Starter code (Pandas (Python))
import pandas as pd
def best_sellers(products: pd.DataFrame, warehouses: pd.DataFrame) -> pd.DataFrame:
# Your code here
return productsSolve this Pandas question free
Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.
Solution & explanation
Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.
Sign up free to unlock