otherwise
Specify a default value for when conditions in when() are not met.
Syntax
Python
otherwise(value)
Parameters
Parameter | Type | Description |
|---|---|---|
| value | Default value to return |
Returns
Column
Examples
Python
from pyspark.sql import functions as sf
df = spark.createDataFrame(
[(2, "Alice"), (5, "Bob")], ["age", "name"])
df.select(df.name, sf.when(df.age > 3, 1).otherwise(0)).show()
Output
# +-----+-------------------------------------+
# | name|CASE WHEN (age > 3) THEN 1 ELSE 0 END|
# +-----+-------------------------------------+
# |Alice| 0|
# | Bob| 1|
# +-----+-------------------------------------+