Condividi tramite


Altrimenti

Specificare un valore predefinito per quando le condizioni in when() non vengono soddisfatte.

Sintassi

otherwise(value)

Parametri

Parametro Tipo Descrizione
value value Valore predefinito da restituire

Restituzioni

colonna

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 > 3, 1).otherwise(0)).show()
# +-----+-------------------------------------+
# | name|CASE WHEN (age > 3) THEN 1 ELSE 0 END|
# +-----+-------------------------------------+
# |Alice|                                    0|
# |  Bob|                                    1|
# +-----+-------------------------------------+