Skip to main content
PYSPARK โ€ข LESSON 75

Calculating Averages with avg()

How do we compute the mean employee salary across the entire organization?

Intermediateโฑ 2 Minutesโญ 370 XP
๐Ÿค” THE QUESTION

How do we compute the mean employee salary across the entire organization?

๐Ÿ’ก WHAT IS IT?

avg() computes the arithmetic mean of numeric column values across all rows.

๐ŸŽฏ WHAT IS IT USED FOR?

Compensation benchmarking, average order value tracking, and performance metrics.

๐Ÿ’ป EXAMPLE
from pyspark.sql.functions import col, avg

df.select(avg(col("salary")).alias("mean_salary")).show()

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Calculating Averages with avg().

  • โœ“ Import avg function
  • โœ“ Compute mean salary
  • โœ“ Display average compensation metric