pyspark.sql.functions.coalesce

pyspark.sql.functions.coalesce(*cols: ColumnOrName) → pyspark.sql.column.Column[source]

Returns the first column that is not null.

New in version 1.4.0.

Changed in version 3.4.0: Supports Spark Connect.

Parameters
colsColumn or str

list of columns to work on.

Returns
Column

value of the first column that is not null.

Examples

>>> cDf = spark.createDataFrame([(None, None), (1, None), (None, 2)], ("a", "b"))
>>> cDf.show()
+----+----+
|   a|   b|
+----+----+
|null|null|
|   1|null|
|null|   2|
+----+----+
>>> cDf.select(coalesce(cDf["a"], cDf["b"])).show()
+--------------+
|coalesce(a, b)|
+--------------+
|          null|
|             1|
|             2|
+--------------+
>>> cDf.select('*', coalesce(cDf["a"], lit(0.0))).show()
+----+----+----------------+
|   a|   b|coalesce(a, 0.0)|
+----+----+----------------+
|null|null|             0.0|
|   1|null|             1.0|
|null|   2|             0.0|
+----+----+----------------+