Premium problem110. Users in the Top Distance Ranks

Medium Locked

users has columns user_id and name. rides has columns ride_id, user_id and distance.

Total the distance each user travelled and return the users within the top top_n dense ranks, as a DataFrame with columns name and distance. Because the rank is dense, a tie at the top can return more than top_n rows. Sort by distance descending then name, and renumber the index from 0.

Input

users =
   user_id name
0        1  Ann
1        2   Bo
2        3   Cy
3        4   Di
rides =
   ride_id  user_id  distance
0        1        1       5.0
1        2        1       6.0
2        3        2      11.0
3        4        3       4.0
4        5        4       1.0
top_n = 2

Output

  name  distance
0  Ann      11.0
1   Bo      11.0
2   Cy       4.0

Premium problem

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

Implement solve(...)