Skip to main content
PYSPARK โ€ข LESSON 287

Scenario: User Retention Cohort Analysis

How can we group users by signup cohort month and evaluate how many return to place orders in month 1, month 2, and month 3?

Productionโฑ 3 Minutesโญ 860 XP
๐Ÿค” THE QUESTION

How can we group users by signup cohort month and evaluate how many return to place orders in month 1, month 2, and month 3?

๐Ÿ’ก WHAT IS IT?

Joining signup dates with order activity dates computes cohort month offsets for retention grid matrices.

๐ŸŽฏ WHAT IS IT USED FOR?

SaaS and subscription analytics measuring customer retention and product-market fit over time.

๐Ÿ’ป EXAMPLE
df_cohorts = df_orders.join(df_users.select("user_id", col("signup_date").alias("cohort_start")), "user_id").withColumn("cohort_month", date_format("cohort_start", "yyyy-MM")).withColumn("activity_month", date_format("order_date", "yyyy-MM")).withColumn("month_offset", months_between(col("order_date"), col("cohort_start")).cast("int"))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Scenario: User Retention Cohort Analysis.

  • โœ“ Cohort analysis calculation
  • โœ“ Months between signup and order
  • โœ“ Retention matrix aggregation