pyspark.sql.functions.nullif#

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

Returns null if col1 equals to col2, or col1 otherwise.

New in version 3.5.0.

Parameters
col1Column or column name
col2Column or column name

Examples

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