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?

Intermediateโฑ 2 Minutesโญ 380 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