Premium problem147. Long to Wide

Medium Locked

metrics has columns user_id, metric and value, with at most one row per user and metric.

Reshape it to one row per user and one column per metric, indexed by user_id with the columns sorted alphabetically and the columns index unnamed. Users missing a metric get NaN.

Because the pairs are unique there is nothing to aggregate, so this is pivot rather than pivot_table; the difference matters, since pivot raises on a duplicate pair instead of silently averaging it away.

Input

   user_id  metric  value
0        1  visits    3.0
1        1   spend   10.0
2        2   spend   20.0
3        2  visits    5.0
4        3  visits    1.0

Output

         spend  visits
user_id               
1         10.0     3.0
2         20.0     5.0
3          NaN     1.0

Premium problem

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

Implement solve(...)