The when
command in Spark is used to apply conditional logic to DataFrame columns. It is often used in conjunction with otherwise
to handle cases where the condition is not met. This is similar to the IF-ELSE
or CASE-WHEN
logic in SQL.
PySpark:
Spark SQL:
value
should be applied.True
.False
.PySpark:
Spark SQL:
Output:
PySpark:
Spark SQL:
Output:
PySpark:
Spark SQL:
Output:
when
with Other FunctionsPySpark:
Spark SQL:
Output:
PySpark:
Spark SQL:
Output:
PySpark:
Spark SQL:
Output:
when
in combination with other functions (e.g., concat
, lit
) for advanced transformations.when
command is used to apply conditional logic to DataFrame columns, similar to IF-ELSE
or CASE-WHEN
in SQL.otherwise
to handle cases where none of the conditions are met.CASE-WHEN
statements.