pyspark.sql.functions.map_values#

pyspark.sql.functions.map_values(col)[source]#

Map function: Returns an unordered array containing the values of the map.

New in version 2.3.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

Name of column or expression

Returns
Column

Values of the map as an array.

Examples

Example 1: Extracting values from a simple map

>>> from pyspark.sql import functions as sf
>>> df = spark.sql("SELECT map(1, 'a', 2, 'b') as data")
>>> df.select(sf.sort_array(sf.map_values("data"))).show()
+----------------------------------+
|sort_array(map_values(data), true)|
+----------------------------------+
|                            [a, b]|
+----------------------------------+

Example 2: Extracting values from a map with complex values

>>> from pyspark.sql import functions as sf
>>> df = spark.sql("SELECT map(1, array('a', 'b'), 2, array('c', 'd')) as data")
>>> df.select(sf.sort_array(sf.map_values("data"))).show()
+----------------------------------+
|sort_array(map_values(data), true)|
+----------------------------------+
|                  [[a, b], [c, d]]|
+----------------------------------+

Example 3: Extracting values from a map with null values

>>> from pyspark.sql import functions as sf
>>> df = spark.sql("SELECT map(1, null, 2, 'b') as data")
>>> df.select(sf.sort_array(sf.map_values("data"))).show()
+----------------------------------+
|sort_array(map_values(data), true)|
+----------------------------------+
|                         [NULL, b]|
+----------------------------------+

Example 4: Extracting values from a map with duplicate values

>>> from pyspark.sql import functions as sf
>>> df = spark.sql("SELECT map(1, 'a', 2, 'a') as data")
>>> df.select(sf.map_values("data")).show()
+----------------+
|map_values(data)|
+----------------+
|          [a, a]|
+----------------+

Example 5: Extracting values from an empty map

>>> from pyspark.sql import functions as sf
>>> df = spark.sql("SELECT map() as data")
>>> df.select(sf.map_values("data")).show()
+----------------+
|map_values(data)|
+----------------+
|              []|
+----------------+