Back to Practice
#0201
Top Product per Category
MediumPandas25 min20 XP
Problem
Given a DataFrame with category, product, and sales, return the product with highest sales in each category.
Why This Matters
Business analysis often asks for the top product, top city, top customer, or top model per segment.
Function Signature
def top_product_per_category(df):
Examples
Example 1
InputA/apple/10, A/avocado/15, B/banana/8
OutputA avocado 15; B banana 8
avocado beats apple in category A, while banana is the only product in category B.
Constraints
- Return the exact requested result.
- Handle common edge cases.
- Keep the solution readable before optimizing.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
InputA/apple/10, A/avocado/15, B/banana/8
ExpectedA avocado 15; B banana 8
avocado beats apple in category A, while banana is the only product in category B.
Hidden Test Categories
empty or minimal inputrepeated valueslarger realistic input