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?

Advanced2 Minutes680 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