pyspark.sql.Column.when¶
- 
Column.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 - 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
- Returns
- Column
- Column representing whether each element of Column is in conditions. 
 
 - See also - Examples - >>> from pyspark.sql import functions as sf >>> df = spark.createDataFrame( ... [(2, "Alice"), (5, "Bob")], ["age", "name"]) >>> df.select(df.name, sf.when(df.age > 4, 1).when(df.age < 3, -1).otherwise(0)).show() +-----+------------------------------------------------------------+ | name|CASE WHEN (age > 4) THEN 1 WHEN (age < 3) THEN -1 ELSE 0 END| +-----+------------------------------------------------------------+ |Alice| -1| | Bob| 1| +-----+------------------------------------------------------------+