Skip to main content
PYSPARK โ€ข LESSON 137

Creating Nested Structures with struct()

How do we group multiple related columns (street, city, zipcode) into a single nested address struct?

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

How do we group multiple related columns (street, city, zipcode) into a single nested address struct?

๐Ÿ’ก WHAT IS IT?

struct() bundles multiple column expressions into a composite StructType column.

๐ŸŽฏ WHAT IS IT USED FOR?

Organizing hierarchical data models, preparing nested JSON payloads, and clean domain boundaries.

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

df = df.withColumn("address", struct(col("street"), col("city"), col("zipcode")))

๐ŸŽฏ Mission Objectives

Practice typing production-grade PySpark code for Creating Nested Structures with struct().

  • โœ“ Import struct function
  • โœ“ Bundle street, city, and zip into struct
  • โœ“ Create nested address column