We're sorry but this app doesn't work properly without JavaScript enabled. Please enable it to continue.

This lesson's interactive features are locked, please to keep using them

Conversion Rates

The "conversion rate" is the percentage of users who complete a desired action, like signing up. If 100 people visit your homepage and 10 sign up, the conversion rate is 10%. If only 5 of those signups submit a lesson, the "step conversion rate" from signup to lesson submission is 50%, but the "overall conversion rate" from homepage visit to lesson submission is only 5%.

Using these funnel_steps and funnel_counts:

funnel_steps = ["page_view", "signup", "lesson_submit", "purchase"]

funnel_counts = {}
eligible_users = None
for step in funnel_steps:
    users_at_step = set(events[events["event_type"] == step]["user_id"])
    eligible_users = (
        users_at_step if eligible_users is None else eligible_users & users_at_step
    )
    funnel_counts[step] = len(eligible_users)

We can make a DataFrame and add columns for overall and step-by-step conversion rates:

funnel_df = pd.DataFrame(
    {"step": funnel_steps, "users": [funnel_counts[s] for s in funnel_steps]}
)

funnel_df["conversion_rate"] = funnel_df["users"] / funnel_df["users"].iloc[0]
funnel_df["step_conversion"] = funnel_df["users"] / funnel_df["users"].shift(1)

Which prints something like:

          step  users  conversion_rate  step_conversion
0      page_view      8            1.000              NaN
1         signup      5            0.625         0.625000
2  lesson_submit      3            0.375         0.600000
3       purchase      2            0.250         0.666667
  • The .iloc[0] selects the number of users at the first step (index 0).
  • The shift(1) method selects the previous row, so users.shift(1) gives the number of users at the previous step.

Assignment

SnackStack's growth team wants a simple, unordered check of how many users appear in one funnel step compared to another.

Complete the calculate_conversion_rate function. It accepts an events DataFrame (user_id and step columns) plus a from_step and a to_step, and returns the conversion rate between them as a float.