Skip to main content
PYSPARK • LESSON 139

Creating Array Collections with array()

How do we combine multiple individual tag columns into an ArrayType collection column?

Advanced2 Minutes700 XP
🤔 THE QUESTION

How do we combine multiple individual tag columns into an ArrayType collection column?

💡 WHAT IS IT?

array(*cols) combines multiple columns of the same type into a single ArrayType collection column.

🎯 WHAT IS IT USED FOR?

Combining categorization tags, multi-category labels, and sequence collections.

💻 EXAMPLE
from pyspark.sql.functions import col, array

df = df.withColumn("tags", array(col("tag1"), col("tag2")))

🎯 Mission Objectives

Practice typing production-grade PySpark code for Creating Array Collections with array().

  • Import array function
  • Combine tag columns into array
  • Construct ArrayType column