AnalystPath

Monthly Retention via Self-Merge

PandasHardSenior levelMeta~15 min

Problem

**[Asked at Meta and Uber]**

For each month, calculate what percentage of that month's active users were
**also active in the previous month** (retained users).

Return `month` (YYYY-MM), `total_users`, `retained_users`, and `retention_pct`
(rounded to 2 dp), ordered by month.

Input data

Example rows — the live problem includes the full dataset.

activity
user_idactivity_date
1012023-01-05
1022023-01-10
1032023-01-15
1012023-02-05
1042023-02-10

Expected output

Your answer should return 3 rows with the columns month, total_users, retained_users, retention_pct.

Starter code (Pandas (Python))

import pandas as pd

def monthly_retention(activity: pd.DataFrame) -> pd.DataFrame:
    # Your code here
    return activity

Solve this Pandas question free

Write Pandas (Python) and run it instantly in your browser — even on your phone. No signup needed to try.

Solution & explanation

Create a free account to unlock the optimal solution, a step-by-step explanation, and the hidden test cases that grade your answer.

Sign up free to unlock

Related Pandas questions