AnalystPath

Top Three Sellers per Warehouse

PandasHardSenior level~10 min

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.

products
product_idproduct_nameunits_soldwarehouse_id
1Stapler851
2Lamp802
3Mug602
4Chair901
5Pen Set691
warehouses
warehouse_idwarehouse_name
1North
2South

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 products

Solve 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

Related Pandas questions