pyspark.sql.functions.when#

pyspark.sql.functions.when(condition, value)[source]#

Evaluates a list of conditions and returns one of multiple possible result expressions. If pyspark.sql.Column.otherwise() is not invoked, None is returned for unmatched conditions.

New in version 1.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
conditionColumn

a boolean Column expression.

value

a literal value, or a Column expression.

Returns
Column

column representing when expression.

Examples

>>> import pyspark.sql.functions as sf
>>> df = spark.range(3)
>>> df.select("*", sf.when(df['id'] == 2, 3).otherwise(4)).show()
+---+------------------------------------+
| id|CASE WHEN (id = 2) THEN 3 ELSE 4 END|
+---+------------------------------------+
|  0|                                   4|
|  1|                                   4|
|  2|                                   3|
+---+------------------------------------+
>>> df.select("*", sf.when(df.id == 2, df.id + 1)).show()
+---+------------------------------------+
| id|CASE WHEN (id = 2) THEN (id + 1) END|
+---+------------------------------------+
|  0|                                NULL|
|  1|                                NULL|
|  2|                                   3|
+---+------------------------------------+