Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Calculating Unit Economics in Python | Unit Economics
Business Analytics and Decision Making with Python

bookCalculating Unit Economics in Python

To effectively analyze your business, you need to calculate and interpret key unit economics metrics: Customer Lifetime Value (LTV), Customer Acquisition Cost (CAC), and Margin. These metrics help you understand how much revenue each customer brings over their relationship with your business, how much it costs to acquire them, and how much profit you retain after costs. Recall the typical formulas:

LTV=AverageΒ RevenueΒ perΒ UserΒ (ARPU)Γ—GrossΒ MarginΒ (%)Γ—AverageΒ CustomerΒ LifespanΒ CAC=TotalΒ MarketingΒ andΒ SalesΒ CostsNumberΒ ofΒ NewΒ CustomersΒ AcquiredΒ Margin=Revenueβˆ’CostΒ ofΒ GoodsΒ SoldRevenue\text{LTV} = \text{Average Revenue per User (ARPU)} \times \text{Gross Margin (\%)} \times \text{Average Customer Lifespan} \\ \ \\ \text{CAC} = \frac{\text{Total Marketing and Sales Costs}}{\text{Number of New Customers Acquired}} \\ \ \\ \text{Margin} = \frac{\text{Revenue} - \text{Cost of Goods Sold}}{\text{Revenue}}

Assume your data is structured in a pandas DataFrame with columns such as customer_id, acquisition_channel, segment, revenue, cost_of_goods_sold, marketing_cost, acquisition_date, and churn_date. This setup allows you to aggregate and segment unit economics by customer characteristics.

1234567891011121314151617181920212223242526272829303132333435363738394041
import pandas as pd # Example customer data data = { 'customer_id': [1, 2, 3, 4, 5], 'revenue': [500, 300, 400, 600, 200], 'cost_of_goods_sold': [300, 180, 220, 350, 120], 'marketing_cost': [50, 40, 60, 70, 30], 'acquisition_date': pd.to_datetime(['2021-01-01', '2021-02-01', '2021-01-15', '2021-03-01', '2021-02-20']), 'churn_date': pd.to_datetime(['2022-01-01', '2021-08-01', '2022-01-15', '2022-03-01', '2021-12-20']) } df = pd.DataFrame(data) # Calculate average lifespan in years df['lifespan_years'] = (df['churn_date'] - df['acquisition_date']).dt.days / 365 # Calculate ARPU (average revenue per user) arpu = df['revenue'].mean() # Calculate gross margin percentage df['gross_margin_pct'] = (df['revenue'] - df['cost_of_goods_sold']) / df['revenue'] avg_gross_margin_pct = df['gross_margin_pct'].mean() # Calculate average customer lifespan avg_lifespan = df['lifespan_years'].mean() # Calculate LTV ltv = arpu * avg_gross_margin_pct * avg_lifespan # Calculate CAC total_marketing_cost = df['marketing_cost'].sum() num_customers = df['customer_id'].nunique() cac = total_marketing_cost / num_customers # Calculate average margin df['margin'] = (df['revenue'] - df['cost_of_goods_sold']) / df['revenue'] avg_margin = df['margin'].mean() print(f"Average LTV: {ltv:.2f}") print(f"Average CAC: {cac:.2f}") print(f"Average Margin: {avg_margin:.2%}")
copy
1234567891011121314151617
# Segmenting unit economics by acquisition channel or customer segment # Suppose you add 'acquisition_channel' and 'segment' columns df['acquisition_channel'] = ['Organic', 'Paid', 'Organic', 'Referral', 'Paid'] df['segment'] = ['SMB', 'Enterprise', 'SMB', 'Enterprise', 'SMB'] # Group by acquisition channel and calculate metrics channel_group = df.groupby('acquisition_channel').agg( arpu=('revenue', 'mean'), gross_margin_pct=('gross_margin_pct', 'mean'), avg_lifespan=('lifespan_years', 'mean'), cac=('marketing_cost', 'mean'), avg_margin=('margin', 'mean') ) channel_group['ltv'] = channel_group['arpu'] * channel_group['gross_margin_pct'] * channel_group['avg_lifespan'] print(channel_group[['arpu', 'gross_margin_pct', 'avg_lifespan', 'ltv', 'cac', 'avg_margin']])
copy

When you segment unit economics by acquisition channel or customer segment, you can spot significant differences in profitability and efficiency. For instance, if customers acquired through organic channels have higher LTV and lower CAC compared to paid channels, this suggests organic growth is more cost-effective. Conversely, if a particular segment has a lower margin, it might indicate higher costs or less pricing power in that group. These insights allow you to prioritize channels and segments that drive sustainable growth, optimize marketing spend, and refine your targeting strategies. Always interpret unit economics in contextβ€”variations may stem from different customer behaviors, product mixes, or marketing approaches.

question mark

Which of the following best describes what you should do if you find that the CAC for your 'Paid' channel is higher than the LTV for customers acquired through that channel?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain how to interpret the output of the segmented unit economics?

How can I use these metrics to make business decisions?

What should I do if I notice large differences between channels or segments?

bookCalculating Unit Economics in Python

Swipe to show menu

To effectively analyze your business, you need to calculate and interpret key unit economics metrics: Customer Lifetime Value (LTV), Customer Acquisition Cost (CAC), and Margin. These metrics help you understand how much revenue each customer brings over their relationship with your business, how much it costs to acquire them, and how much profit you retain after costs. Recall the typical formulas:

LTV=AverageΒ RevenueΒ perΒ UserΒ (ARPU)Γ—GrossΒ MarginΒ (%)Γ—AverageΒ CustomerΒ LifespanΒ CAC=TotalΒ MarketingΒ andΒ SalesΒ CostsNumberΒ ofΒ NewΒ CustomersΒ AcquiredΒ Margin=Revenueβˆ’CostΒ ofΒ GoodsΒ SoldRevenue\text{LTV} = \text{Average Revenue per User (ARPU)} \times \text{Gross Margin (\%)} \times \text{Average Customer Lifespan} \\ \ \\ \text{CAC} = \frac{\text{Total Marketing and Sales Costs}}{\text{Number of New Customers Acquired}} \\ \ \\ \text{Margin} = \frac{\text{Revenue} - \text{Cost of Goods Sold}}{\text{Revenue}}

Assume your data is structured in a pandas DataFrame with columns such as customer_id, acquisition_channel, segment, revenue, cost_of_goods_sold, marketing_cost, acquisition_date, and churn_date. This setup allows you to aggregate and segment unit economics by customer characteristics.

1234567891011121314151617181920212223242526272829303132333435363738394041
import pandas as pd # Example customer data data = { 'customer_id': [1, 2, 3, 4, 5], 'revenue': [500, 300, 400, 600, 200], 'cost_of_goods_sold': [300, 180, 220, 350, 120], 'marketing_cost': [50, 40, 60, 70, 30], 'acquisition_date': pd.to_datetime(['2021-01-01', '2021-02-01', '2021-01-15', '2021-03-01', '2021-02-20']), 'churn_date': pd.to_datetime(['2022-01-01', '2021-08-01', '2022-01-15', '2022-03-01', '2021-12-20']) } df = pd.DataFrame(data) # Calculate average lifespan in years df['lifespan_years'] = (df['churn_date'] - df['acquisition_date']).dt.days / 365 # Calculate ARPU (average revenue per user) arpu = df['revenue'].mean() # Calculate gross margin percentage df['gross_margin_pct'] = (df['revenue'] - df['cost_of_goods_sold']) / df['revenue'] avg_gross_margin_pct = df['gross_margin_pct'].mean() # Calculate average customer lifespan avg_lifespan = df['lifespan_years'].mean() # Calculate LTV ltv = arpu * avg_gross_margin_pct * avg_lifespan # Calculate CAC total_marketing_cost = df['marketing_cost'].sum() num_customers = df['customer_id'].nunique() cac = total_marketing_cost / num_customers # Calculate average margin df['margin'] = (df['revenue'] - df['cost_of_goods_sold']) / df['revenue'] avg_margin = df['margin'].mean() print(f"Average LTV: {ltv:.2f}") print(f"Average CAC: {cac:.2f}") print(f"Average Margin: {avg_margin:.2%}")
copy
1234567891011121314151617
# Segmenting unit economics by acquisition channel or customer segment # Suppose you add 'acquisition_channel' and 'segment' columns df['acquisition_channel'] = ['Organic', 'Paid', 'Organic', 'Referral', 'Paid'] df['segment'] = ['SMB', 'Enterprise', 'SMB', 'Enterprise', 'SMB'] # Group by acquisition channel and calculate metrics channel_group = df.groupby('acquisition_channel').agg( arpu=('revenue', 'mean'), gross_margin_pct=('gross_margin_pct', 'mean'), avg_lifespan=('lifespan_years', 'mean'), cac=('marketing_cost', 'mean'), avg_margin=('margin', 'mean') ) channel_group['ltv'] = channel_group['arpu'] * channel_group['gross_margin_pct'] * channel_group['avg_lifespan'] print(channel_group[['arpu', 'gross_margin_pct', 'avg_lifespan', 'ltv', 'cac', 'avg_margin']])
copy

When you segment unit economics by acquisition channel or customer segment, you can spot significant differences in profitability and efficiency. For instance, if customers acquired through organic channels have higher LTV and lower CAC compared to paid channels, this suggests organic growth is more cost-effective. Conversely, if a particular segment has a lower margin, it might indicate higher costs or less pricing power in that group. These insights allow you to prioritize channels and segments that drive sustainable growth, optimize marketing spend, and refine your targeting strategies. Always interpret unit economics in contextβ€”variations may stem from different customer behaviors, product mixes, or marketing approaches.

question mark

Which of the following best describes what you should do if you find that the CAC for your 'Paid' channel is higher than the LTV for customers acquired through that channel?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 3. ChapterΒ 2
some-alt