Premium problem113. Best Rating per Search

Medium Locked

searches has columns search_id, clicked (boolean) and search_position. One search can produce several rows.

Rate every row:

  • 1 when the result was not clicked
  • 2 when it was clicked and search_position is greater than 3
  • 3 when it was clicked and search_position is 3 or less

Return a DataFrame with columns search_id and rating holding the highest rating each search achieved, sorted by search_id with the index renumbered from 0. Build the rating without a Python loop.

Input

   search_id  clicked  search_position
0          1    False                1
1          1     True                7
2          2     True                2
3          2    False                5
4          3    False                9

Output

   search_id  rating
0          1       2
1          2       3
2          3       1

Premium problem

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

Implement solve(...)