Skip to main content
PYSPARK • LESSON 75

Calculating Averages with avg()

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

Intermediate2 Minutes370 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