subtract two columns with null in spark dataframe

后端 未结 2 389
耶瑟儿~
耶瑟儿~ 2021-01-14 23:19

I new to spark, I have dataframe df:

+----------+------------+-----------+
| Column1  | Column2    | Sub       |                          
+----------+------         


        
2条回答
  •  深忆病人
    2021-01-14 23:59

    You can use when function as

    import org.apache.spark.sql.functions._
    df.withColumn("Sub", when(col("Column1").isNull, lit(0)).otherwise(col("Column1")) - when(col("Column2").isNull, lit(0)).otherwise(col("Column2")))
    

    you should have final result as

    +-------+-------+----+
    |Column1|Column2| Sub|
    +-------+-------+----+
    |      1|      2|-1.0|
    |      4|   null| 4.0|
    |      5|   null| 5.0|
    |      6|      8|-2.0|
    +-------+-------+----+
    

提交回复
热议问题