pyspark.sql.functions.when

pyspark.sql.functions.when(condition: pyspark.sql.column.Column, value: Any) → pyspark.sql.column.Column[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

>>> df = spark.range(3)
>>> df.select(when(df['id'] == 2, 3).otherwise(4).alias("age")).show()
+---+
|age|
+---+
|  4|
|  4|
|  3|
+---+
>>> df.select(when(df.id == 2, df.id + 1).alias("age")).show()
+----+
| age|
+----+
|NULL|
|NULL|
|   3|
+----+