Back to Practice
#0100
Revenue Share Within Group
MediumPandas25 min20 XP
Problem
Given a DataFrame with category and revenue, return a copy with revenue_share equal to row revenue divided by total revenue of its category.
Why This Matters
transform is essential when you need group-level statistics aligned back to original rows.
Function Signature
def add_revenue_share(df):
Examples
Example 1
InputA: 20, A: 30, B: 10
OutputA shares 0.4 and 0.6; B share 1.0
Each row divides by its category total.
Constraints
- Return the exact structure requested.
- Handle the edge cases mentioned in the prompt.
- Prefer clear logic over clever one-liners.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
InputA: 20, A: 30, B: 10
ExpectedA shares 0.4 and 0.6; B share 1.0
Each row divides by its category total.
Hidden Test Categories
single row groupdecimal revenueinput not mutated