Skip to main content
PYSPARK • LESSON 142

Key-Value Dictionaries with create_map()

How do we construct a dynamic MapType column containing key-value pairs for environment and tier?

Advanced2 Minutes750 XP
🤔 THE QUESTION

How do we construct a dynamic MapType column containing key-value pairs for environment and tier?

💡 WHAT IS IT?

create_map(*cols) creates a MapType column from alternating key and value expressions.

🎯 WHAT IS IT USED FOR?

Dynamic attribute storage, unstructured metadata tagging, and key-value feature extraction.

💻 EXAMPLE
from pyspark.sql.functions import col, create_map, lit

df = df.withColumn("attributes", create_map(lit("env"), col("env"), lit("tier"), col("tier")))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Key-Value Dictionaries with create_map().

  • Import create_map and lit
  • Construct key-value pairs in MapType
  • Store dynamic attributes column