pyspark.sql.functions.map_contains_key#

pyspark.sql.functions.map_contains_key(col, value)[source]#

Map function: Returns true if the map contains the key.

New in version 3.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colColumn or str

The name of the column or an expression that represents the map.

value

A literal value.

Returns
Column

True if key is in the map and False otherwise.

Examples

Example 1: The key is in the map

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

Example 2: The key is not in the map

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