pyspark.sql.functions.contains#

pyspark.sql.functions.contains(left, right)[source]#

Returns a boolean. The value is True if right is found inside left. Returns NULL if either input expression is NULL. Otherwise, returns False. Both left or right must be of STRING or BINARY type.

Added in version 3.5.0.

Parameters:
leftColumn or str

The input to check; may be NULL. A column that evaluates to a string or binary.

rightColumn or str

The value to find; may be NULL. A column that evaluates to a string or binary.

Examples

>>> df = spark.createDataFrame([("Spark SQL", "Spark")], ['a', 'b'])
>>> df.select(contains(df.a, df.b).alias('r')).collect()
[Row(r=True)]
>>> df = spark.createDataFrame([("414243", "4243",)], ["c", "d"])
>>> df = df.select(to_binary("c").alias("c"), to_binary("d").alias("d"))
>>> df.printSchema()
root
 |-- c: binary (nullable = true)
 |-- d: binary (nullable = true)
>>> df.select(contains("c", "d"), contains("d", "c")).show()
+--------------+--------------+
|contains(c, d)|contains(d, c)|
+--------------+--------------+
|          true|         false|
+--------------+--------------+