Compute the user-level conversion rate per A/B test variant from session-level data in pandas

Quick Overview

Write a pandas function that turns one-row-per-session experiment data into a user-level conversion rate for each variant. It tests matching the analysis unit to the randomization unit, groupby rollups, an exact output contract and why session-level averages distort an A/B test readout.

Compute the user-level conversion rate per A/B test variant from session-level data in pandas

Role: Data Scientist

Category: Analytics & Experimentation

Difficulty: easy

Interview Round: Other

Implement `conversion_rate_by_variant(df: pd.DataFrame) -> pd.DataFrame`. `df` has one row per session with columns `user_id` (object), `variant` (object, `'control'` or `'treatment'`), and `converted` (int64, 0/1). Return each variant's user-level conversion rate. Requirements: - A user's flag is 1 if they converted in any session (`groupby('user_id')['converted'].max()`), and the flag carries that user's variant. - `conversion_rate` is the mean of the user-level flags per variant (not the row-level `converted` mean). - Return exactly the columns `['variant', 'conversion_rate']`, one row per variant. - Sort by `variant` ascending and reset the index to `0..k-1`. - Dtypes: `variant` object, `conversion_rate` float64. Example: ```text Input sessions (user_id, variant, converted): u1, control, 0 u1, control, 1 u2, control, 0 u3, treatment, 1 Output: variant conversion_rate 0 control 0.5 1 treatment 1.0 ``` User u1 converted in one of two sessions, so u1's flag is 1. User u2 never converted, so control's rate is (1 + 0) / 2 = 0.5. Treatment has one user, who converted, so its rate is 1.0. ```hint Pick the unit first Decide what counts as one observation before you average anything. The unit that was randomized tells you. ``` ### Constraints & Assumptions - `df` has one row per session, so a user can have many rows. - Each user belongs to exactly one variant across all their sessions. - `converted` is clean 0/1 with no missing values. ### What a Strong Answer Covers - Why the user is the unit of analysis here and what goes wrong with the session-level mean. - The per-user rollup (a conversion in any session counts) and how each user's variant is carried along. - The output contract: the exact columns, one row per variant, the ascending sort, the reset index and the dtypes. - A check against the example, including how the session-level number would differ. ### Follow-up Questions - Why does the session-level mean bias the result, and when could it match the user-level rate? - How would you add a confidence interval or significance test on the user-level rates? - What would you do if some users appeared under both variants?

Overview: Write a pandas function that turns one-row-per-session experiment data into a user-level conversion rate for each variant. It tests matching the analysis unit to the randomization unit, groupby rollups, an exact output contract and why session-level averages distort an A/B test readout.

PracHub logo
General
Nov 25, 2026
easyData ScientistOtherAnalytics & Experimentation
0
0

Implement conversion_rate_by_variant(df: pd.DataFrame) -> pd.DataFrame. df has one row per session with columns user_id (object), variant (object, 'control' or 'treatment'), and converted (int64, 0/1). Return each variant's user-level conversion rate.

Requirements:

  • A user's flag is 1 if they converted in any session ( groupby('user_id')['converted'].max() ), and the flag carries that user's variant.
  • conversion_rate is the mean of the user-level flags per variant (not the row-level converted mean).
  • Return exactly the columns ['variant', 'conversion_rate'] , one row per variant.
  • Sort by variant ascending and reset the index to 0..k-1 .
  • Dtypes: variant object, conversion_rate float64.

Example:

Input sessions (user_id, variant, converted):
u1, control,   0
u1, control,   1
u2, control,   0
u3, treatment, 1

Output:
     variant  conversion_rate
0    control              0.5
1  treatment              1.0

User u1 converted in one of two sessions, so u1's flag is 1. User u2 never converted, so control's rate is (1 + 0) / 2 = 0.5. Treatment has one user, who converted, so its rate is 1.0.

Constraints & Assumptions

  • df has one row per session, so a user can have many rows.
  • Each user belongs to exactly one variant across all their sessions.
  • converted is clean 0/1 with no missing values.

What a Strong Answer Covers Guidance

  • Why the user is the unit of analysis here and what goes wrong with the session-level mean.
  • The per-user rollup (a conversion in any session counts) and how each user's variant is carried along.
  • The output contract: the exact columns, one row per variant, the ascending sort, the reset index and the dtypes.
  • A check against the example, including how the session-level number would differ.

Follow-up Questions Guidance

  • Why does the session-level mean bias the result, and when could it match the user-level rate?
  • How would you add a confidence interval or significance test on the user-level rates?
  • What would you do if some users appeared under both variants?