Cohort Table Construction in Pandas
Build retention-style cohort tables.
The Open buttons send the prompt text as currently shown below to that site only when you click. Fill in the placeholders first.
Show me how to build a cohort table with pandas. My event data columns: [COLUMNS]. Definition of cohort and activity: [DEFINITIONS]. Period: [PERIOD]. Provide the steps with code: assign each user to a cohort by first activity, compute periods since cohort start, pivot to a cohort by period matrix, and convert to retention percentages. Include handling of users with multiple events in a period, time zones, and incomplete current periods. Produce checks that cohort sizes match the number of unique users and that the first period is 100 percent. Show how to plot the table as a heatmap with readable labels and how to explain it in two sentences. Use a tiny made-up dataset in the explanation. Add a ten-line invented event table and the cohort table it should produce, so that I can test the code against a known answer. Include notes on how to extend the table to revenue cohorts, how to handle reactivated users, and a short commentary template for interpreting what a flat versus a declining curve implies for the product.
Fill in before use
Optional: type your details and the prompt above updates. Nothing is saved or sent.
Replace each bracketed placeholder with your own details, then paste into Any AI or another assistant.
See an example result (fictional data)
Tiny dataset: 6 users, events in Jan-Mar.
cohort (first month) | M0 | M1 | M2
Jan (3 users) | 100% | 67% | 33%
Feb (2 users) | 100% | 50% | n/a
Mar (1 user) | 100% | n/a | n/a
Steps: first = df.groupby("user")["month"].transform("min"); period = months between month and first; pivot users by (first, period); divide by the M0 column.
Checks: cohort size equals unique users; M0 is 100%.
Reading: Jan loses a third in the first month, then a third again. Incomplete current months must not be shown as zero.Illustrative only, made-up data. Your results will differ and must be reviewed.