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?

Advancedโฑ 2 Minutesโญ 700 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