【问题标题】:How can i use nvl function in scala我如何在 scala 中使用 nvl 函数
【发布时间】:2023-01-05 16:48:40
【问题描述】:

我正在尝试编写以下代码:

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

当我执行这个时,我得到一个错误值 nvl not found。

请帮我解决这个问题。

【问题讨论】:

    标签: scala apache-spark pyspark apache-spark-sql


    【解决方案1】:

    在 Spark 中它被称为 coalesce,你可以查看这个article 了解更多细节

    # 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|
    +----+------+---------+
    

    【讨论】:

      猜你喜欢
      • 2021-08-23
      • 2017-11-06
      • 1970-01-01
      • 2018-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-09
      相关资源
      最近更新 更多