How do we compute rounded percentage of revenue contributed by each product within its department?
Combining partition window sums, multiplication by 100, and the round() function to calculate percentage shares.
Merchandising revenue concentration analysis and identifying top revenue drivers.
from pyspark.sql.window import Window
from pyspark.sql.functions import col, round, sum
dept_window = Window.partitionBy("department")
df = df.withColumn("pct_dept_share", round(col("revenue") / sum(col("revenue")).over(dept_window) * 100, 2))Practice typing production-grade PySpark code for Cumulative Percentage of Total Share.