pyspark.sql.functions.create_map

pyspark.sql.functions.create_map(*cols: Union[ColumnOrName, List[ColumnOrName_], Tuple[ColumnOrName_, …]]) → pyspark.sql.column.Column[source]

Creates a new map column.

New in version 2.0.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colsColumn or str

column names or Columns that are grouped as key-value pairs, e.g. (key1, value1, key2, value2, …).

Examples

>>> df = spark.createDataFrame([("Alice", 2), ("Bob", 5)], ("name", "age"))
>>> df.select(create_map('name', 'age').alias("map")).collect()
[Row(map={'Alice': 2}), Row(map={'Bob': 5})]
>>> df.select(create_map([df.name, df.age]).alias("map")).collect()
[Row(map={'Alice': 2}), Row(map={'Bob': 5})]