Back to Practice
#0171

Percent of Group Total

MediumPandas25 min20 XP

Problem

Given category and amount columns, add share_in_category = amount divided by total amount for that category.

Why This Matters

Percent-of-total features are common in sales reporting, customer behavior, and model features.

Function Signature

def add_group_share(df):

Examples

Example 1
Inputcategory A amounts 20 and 80; category B amount 50
OutputA shares 0.2 and 0.8; B share 1.0

transform returns one total per original row, so division aligns naturally.

Constraints

  • Return the exact requested output.
  • Handle ordinary edge cases.
  • Prefer simple, interview-readable code.
CodePython
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core example
Inputcategory A amounts 20 and 80; category B amount 50
ExpectedA shares 0.2 and 0.8; B share 1.0

transform returns one total per original row, so division aligns naturally.

Hidden Test Categories
empty or minimal inputduplicates or tieslarger input