Premium problem92. Month-Over-Month User Growth per Account

Hard Locked

events has columns account_id, user_id and event_date (datetime). A user who was active several times in a month still counts once.

first_month and second_month are month strings like "2020-12". Return a DataFrame with columns account_id, first_count, second_count and growth_pct, covering every account that appears in either month, where growth_pct is the percentage change from the first month to the second, rounded to 2 decimal places.

An account with no users in the first month has no defined growth: give it NaN rather than infinity. Sort by account_id and renumber the index from 0.

Input

events =
  account_id  user_id event_date
0          A        1 2020-12-05
1          A        2 2020-12-06
2          A        1 2021-01-09
3          B        3 2020-12-11
4          B        3 2021-01-02
5          C        4 2021-01-15
first_month = '2020-12'
second_month = '2021-01'

Output

  account_id  first_count  second_count  growth_pct
0          A            2             1       -50.0
1          B            1             1         0.0
2          C            0             1         NaN

Premium problem

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

Implement solve(...)