pyspark.sql.functions.
when
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.
pyspark.sql.Column.otherwise()
New in version 1.4.0.
Changed in version 3.4.0: Supports Spark Connect.
Column
a boolean Column expression.
a literal value, or a Column expression.
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| +----+