Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Returns an unordered array containing the keys of the map.
Syntax
from pyspark.sql import functions as sf
sf.map_keys(col)
Parameters
| Parameter | Type | Description |
|---|---|---|
col |
pyspark.sql.Column or str |
Name of column or expression |
Returns
pyspark.sql.Column: Keys of the map as an array.
Examples
Example 1: Extracting keys 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_keys("data"))).show()
+--------------------------------+
|sort_array(map_keys(data), true)|
+--------------------------------+
| [1, 2]|
+--------------------------------+
Example 2: Extracting keys from a map with complex keys
from pyspark.sql import functions as sf
df = spark.sql("SELECT map(array(1, 2), 'a', array(3, 4), 'b') as data")
df.select(sf.sort_array(sf.map_keys("data"))).show()
+--------------------------------+
|sort_array(map_keys(data), true)|
+--------------------------------+
| [[1, 2], [3, 4]]|
+--------------------------------+
Example 3: Extracting keys from an empty map
from pyspark.sql import functions as sf
df = spark.sql("SELECT map() as data")
df.select(sf.map_keys("data")).show()
+--------------+
|map_keys(data)|
+--------------+
| []|
+--------------+