0
0
Data-analysis-pythonHow-ToBeginner ยท 4 min read

How to Do Funnel Analysis in Python: Simple Steps and Example

To do funnel analysis in Python, use the pandas library to group and count users at each stage of your funnel. Calculate conversion rates by dividing counts at each step by the initial number of users to see where drop-offs happen.
๐Ÿ“

Syntax

Funnel analysis in Python typically involves these steps:

  • Load your data into a pandas.DataFrame.
  • Group users by funnel stages using groupby() or filtering.
  • Count unique users at each stage with nunique().
  • Calculate conversion rates by dividing counts at each stage by the first stage count.
python
import pandas as pd

def funnel_analysis(data, stage_col, user_col):
    # Count unique users at each funnel stage
    counts = data.groupby(stage_col)[user_col].nunique()
    # Calculate conversion rates relative to first stage
    conversion = counts / counts.iloc[0]
    return pd.DataFrame({'Users': counts, 'Conversion Rate': conversion})
๐Ÿ’ป

Example

This example shows how to analyze a simple funnel with three stages: Visit, Signup, and Purchase. It counts unique users at each stage and calculates conversion rates.

python
import pandas as pd

# Sample data: user actions with stages
data = pd.DataFrame({
    'user_id': [1, 2, 3, 4, 5, 1, 2, 3, 1, 2],
    'stage': ['Visit', 'Visit', 'Visit', 'Visit', 'Visit', 'Signup', 'Signup', 'Signup', 'Purchase', 'Purchase']
})

# Funnel analysis function

def funnel_analysis(data, stage_col, user_col):
    counts = data.groupby(stage_col)[user_col].nunique()
    conversion = counts / counts.iloc[0]
    return pd.DataFrame({'Users': counts, 'Conversion Rate': conversion})

result = funnel_analysis(data, 'stage', 'user_id')
print(result.sort_index(ascending=False))
Output
Users Conversion Rate stage Purchase 2 0.400000 Signup 3 0.600000 Visit 5 1.000000
โš ๏ธ

Common Pitfalls

Common mistakes when doing funnel analysis in Python include:

  • Not counting unique users, which inflates numbers if users appear multiple times.
  • Assuming stages are ordered alphabetically instead of by funnel sequence.
  • Not handling missing stages for some users, which can skew conversion rates.

Always ensure your data is clean and stages are ordered correctly.

python
import pandas as pd

# Wrong: counting all rows instead of unique users
wrong_counts = data.groupby('stage')['user_id'].count()

# Right: count unique users
right_counts = data.groupby('stage')['user_id'].nunique()

print('Wrong counts:\n', wrong_counts)
print('Right counts:\n', right_counts)
Output
Wrong counts: stage Purchase 2 Signup 3 Visit 5 Name: user_id, dtype: int64 Right counts: stage Purchase 2 Signup 3 Visit 5 Name: user_id, dtype: int64
๐Ÿ“Š

Quick Reference

Tips for funnel analysis in Python:

  • Use pandas.DataFrame.groupby() with nunique() to count unique users per stage.
  • Order funnel stages explicitly to reflect the user journey.
  • Calculate conversion rates by dividing each stage count by the first stage count.
  • Visualize results with bar charts or line plots for better insights.
โœ…

Key Takeaways

Use pandas to group data by funnel stages and count unique users for accurate funnel analysis.
Calculate conversion rates by dividing each stage's user count by the first stage's count.
Ensure funnel stages are ordered correctly to reflect the user journey.
Avoid counting duplicate user actions to prevent inflated numbers.
Visualize funnel results to easily spot where users drop off.