简体   繁体   中英

How can i use nvl function in scala

I am trying to code the following:

df.select(nvl(col("id"),0))

When I execute this, I get an error value nvl not found.

Please help me to solve this issue.

In Spark its called coalesce, you can check this article for more details

# create new column with non Null values
tmp = testDF.withColumn('newColumn', coalesce(testDF['id'], testDF['number']))

# Check the content of new df
tmp.show()

+----+------+---------+
|  id|number|newColumn|
+----+------+---------+
|   1|     1|        1|
|   2|     2|        2|
|null|     3|        3|
|   4|  null|        4|
+----+------+---------+

In your case it may look like this:

df.select(coalesce(col("id"),lit(0)))

You can use a when-otherwise construct as well - see the snippet below:

df = spark.createDataFrame([(1, 2), (2, None), (None, 3)], "id: int, value: int")
df.withColumn("non_null_value", when(col("value").isNull(), 0).otherwise(col("value"))).show()

+----+-----+--------------+
|  id|value|non_null_value|
+----+-----+--------------+
|   1|    2|             2|
|   2| null|             0|
|null|    3|             3|
+----+-----+--------------+

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM