Skip to main content
PYSPARK • LESSON 81

Basic Grouping with groupBy()

How do we group employees by department and count the headcount in each department?

Intermediate2 Minutes380 XP
🤔 THE QUESTION

How do we group employees by department and count the headcount in each department?

💡 WHAT IS IT?

groupBy(col).count() groups records by identical column values and computes row frequencies per group.

🎯 WHAT IS IT USED FOR?

Headcount reporting, event frequency analysis, and category volume benchmarking.

💻 EXAMPLE
df.groupBy("department").count().show()

🎯 Mission Objectives

Practice typing production-grade PySpark code for Basic Grouping with groupBy().

  • Group records by department
  • Compute row counts per group
  • Display departmental headcount