Skip to main content
PYSPARK โ€ข LESSON 140

Flattening Array Collections with explode()

How do we unnest an array of order items into individual rows, one row per item?

Advancedโฑ 2 Minutesโญ 720 XP
๐Ÿค” THE QUESTION

How do we unnest an array of order items into individual rows, one row per item?

๐Ÿ’ก WHAT IS IT?

explode(col) creates a new row for each element in the given array or map column.

๐ŸŽฏ WHAT IS IT USED FOR?

Unnesting shopping carts, normalising 1-to-many relationship payloads, and processing clickstream sequences.

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

exploded_df = df.select(col("order_id"), explode(col("items")).alias("item"))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Flattening Array Collections with explode().

  • โœ“ Import explode function
  • โœ“ Unnest order items array into rows
  • โœ“ Alias individual item records