Premium problem117. Top Three Products in Each Category

Medium Locked

products has columns product, category and revenue.

Within each category, return the products sitting in the top three dense ranks by revenue, so ties do not consume a rank and a category can return more than three rows. Keep the original columns, sort by category then revenue descending then product, and renumber the index from 0.

Input

  product category  revenue
0       a        X     12.0
1       b        X     10.0
2       c        X      9.0
3       d        X      9.0
4       e        X      1.0
5       f        Y      5.0
6       g        Y      4.0
7       h        Y      3.0

Output

  product category  revenue
0       a        X     12.0
1       b        X     10.0
2       c        X      9.0
3       d        X      9.0
4       f        Y      5.0
5       g        Y      4.0
6       h        Y      3.0

Premium problem

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

Implement solve(...)