Premium problem128. Best-Selling Product in Each Category

Medium Locked

sales has columns category, product and units.

Total the units per product and return the best seller in each category as a DataFrame with columns category, product and units. A category with a tie returns every tied product. Sort by category then product, and renumber the index from 0.

idxmax picks one winner arbitrarily when there is a tie, so it is the wrong tool for the last sentence.

Input

  category product  units
0        X       a      2
1        X       a      3
2        X       b      5
3        Y       c      7
4        Y       d      7

Output

  category product  units
0        X       a      5
1        X       b      5
2        Y       c      7
3        Y       d      7

Premium problem

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

Implement solve(...)