How do we compute headcount, total spend, average comp, and maximum salary per department in one pass?
Passing multiple aggregate functions into agg() to calculate comprehensive group statistics in a single shuffle.
Departmental budget audits, organizational compensation analysis, and executive HR reports.
from pyspark.sql.functions import avg, col, count, max, sum
df.groupBy("department").agg(
count(col("emp_id")).alias("headcount"),
sum(col("salary")).alias("dept_spend"),
avg(col("salary")).alias("avg_comp"),
max(col("salary")).alias("top_salary")
).show()Practice typing production-grade PySpark code for Custom Multi-Metric Aggregations with agg().