pyspark.sql.functions.nvl2#

pyspark.sql.functions.nvl2(col1, col2, col3)[source]#

Returns col2 if col1 is not null, or col3 otherwise.

New in version 3.5.0.

Parameters
col1Column or column name
col2Column or column name
col3Column or column name

Examples

>>> import pyspark.sql.functions as sf
>>> df = spark.createDataFrame([(None, 8, 6,), (1, 9, 9,)], ["a", "b", "c"])
>>> df.select('*', sf.nvl2(df.a, df.b, df.c)).show()
+----+---+---+-------------+
|   a|  b|  c|nvl2(a, b, c)|
+----+---+---+-------------+
|NULL|  8|  6|            6|
|   1|  9|  9|            9|
+----+---+---+-------------+
>>> df.select('*', sf.nvl2('a', 'b', 'c')).show()
+----+---+---+-------------+
|   a|  b|  c|nvl2(a, b, c)|
+----+---+---+-------------+
|NULL|  8|  6|            6|
|   1|  9|  9|            9|
+----+---+---+-------------+