Premium problem160. Monthly Cohort Retention

Hard Locked

events has columns user_id and event_date (datetime).

A user's cohort is the month of their first activity. Build the retention table: rows indexed by cohort month (as a "YYYY-MM" string, index named cohort), columns 0, 1, 2, ... counting months since that cohort started (columns index unnamed), and cells holding the percentage of the cohort active in that month, rounded to 2 decimal places.

Column 0 is always 100.0, since every member is active in their own first month. Combinations nobody reached are NaN. The month offset is a count of calendar months, not a count of days divided by 30.

Input

   user_id event_date
0        1 2022-01-05
1        1 2022-02-07
2        1 2022-03-02
3        2 2022-01-09
4        2 2022-03-15
5        3 2022-02-01
6        3 2022-02-20

Output

             0     1      2
cohort                     
2022-01  100.0  50.0  100.0
2022-02  100.0   NaN    NaN

Premium problem

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

Implement solve(...)