Skip to content

Instantly share code, notes, and snippets.

@1ambda
Created December 21, 2021 15:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 1ambda/9c97469c04775b0017b89dfc651894a3 to your computer and use it in GitHub Desktop.
Save 1ambda/9c97469c04775b0017b89dfc651894a3 to your computer and use it in GitHub Desktop.
df\
    .selectExpr("brand", "price", "user_id", "event_type")\
    .where("brand IS NOT NULL")\
    .groupBy("brand")\
    .agg(
        countDistinct("user_id").alias("user_count_all"),
        countDistinct(when(col("event_type") == lit(eventPurchase), col("user_id"))).alias("user_count_purchase"),
        sum("price").alias("sales_price"))\
    .selectExpr(
        "brand",
        "sales_price",
        "user_count_all",
        "sales_price / user_count_all as ARPU",
        "user_count_purchase",
        "sales_price / user_count_purchase as ARPPU",
    )\
    .orderBy(desc("sales_price"))\
    .limit(10)\
    .show(truncate=False)
+--------+------------------+--------------+------------------+-------------------+------------------+
|brand |sales_price |user_count_all|ARPU |user_count_purchase|ARPPU |
+--------+------------------+--------------+------------------+-------------------+------------------+
|strong |2651513.6799999983|5683 |466.5693612528591 |186 |14255.449892473109|
|jessnail|2297451.200000003 |21039 |109.19963876610119|1119 |2053.1288650580905|
|runail |2108654.9900000114|59595 |35.38308566154898 |9096 |231.8222284520681 |
|irisk |1467889.08999999 |48028 |30.56319417839573 |7277 |201.71624158306858|
|grattol |1055984.4699999897|34884 |30.271312636165284|4732 |223.15817202028524|
|marathon|789238.6300000004 |2871 |274.9002542668061 |38 |20769.437631578956|
|masura |693152.7399999842 |22668 |30.578469207692965|3085 |224.6848427876772 |
|cnd |599136.3200000003 |9129 |65.63000547705118 |597 |1003.578425460637 |
|uno |546019.0499999991 |16370 |33.35485949908364 |2446 |223.2293744889612 |
|estel |544366.7999999983 |24438 |22.27542352074631 |2130 |255.571267605633 |
+--------+------------------+--------------+------------------+-------------------+------------------+
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment