Pandasdrill

Level 7: Interview cases Exercise 102 of 102

A conversion funnel

A pandas exercise from Pandasdrill. Solve it with a Python editor in your browser running real pandas, and it is checked the moment you submit.

The task

Product asks for the funnel: how many different users reached each step, and what share of the previous step that is. Return three rows in the order visit, signup, purchase, with the columns `step`, `users` and `rate`. The rate of the first step is 1.0. Some users triggered the same step more than once.

What this exercise teaches

nunique counts each user once per step, which matters because repeated events would inflate the funnel. groupby sorts the steps alphabetically, so reindex puts them back in funnel order before shift lines each step up with the previous one. Dividing by the first step instead would give the share of all visitors, which is a different rate.

A hint if you get stuck

Count distinct users per step, put the steps in funnel order, then divide each count by the one before it.

Try it now, free, no account needed

Your first 8 exercises on Pandasdrill are free. Open it on the practice page, solve it, and get instant feedback with a hint. A free account adds the worked solution; Pro unlocks every exercise, timed mode and the certificate.

Open this exercise

More exercises in Interview cases