Premium problem124. Each Product's Share of Its Category

Medium Locked

products has columns product, category and revenue.

Add a category_share column holding each product's revenue divided by the total revenue of its category, so the shares within a category sum to 1. Return the original columns plus category_share, index unchanged.

The denominator has to be broadcast back onto every row, which is what transform is for.

Input

  product category  revenue
0       a        X     30.0
1       b        X     70.0
2       c        Y     10.0
3       d        Y     10.0

Output

  product category  revenue  category_share
0       a        X     30.0             0.3
1       b        X     70.0             0.7
2       c        Y     10.0             0.5
3       d        Y     10.0             0.5

Premium problem

This one's part of Premium. Unlock the full Pandas track plus every other premium problem on the site.

Implement solve(...)