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
- 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| +--------------------------+
Example 3: Check for key using a column
>>> from pyspark.sql import functions as sf >>> df = spark.sql("SELECT map(1, 'a', 2, 'b') as data, 1 as key") >>> df.select(sf.map_contains_key("data", sf.col("key"))).show() +---------------------------+ |map_contains_key(data, key)| +---------------------------+ | true| +---------------------------+