Spark "replacing null with 0" 性能比较

Spark "replacing null with 0" performance comparison

Spark 1.6.1,Scala api。

对于数据框,我需要将特定列的所有空值替换为 0。 我有两种方法可以做到这一点。 1.

myDF.withColumn("pipConfidence", when($"mycol".isNull, 0).otherwise($"mycol"))

2.

myDF.na.fill(0, Seq("mycol"))

它们本质上是相同的还是首选一种方式?

谢谢!

它们不一样,但性能应该相似。 na.fill 使用 coalesce 但它取代了 NaNNULLs,而不仅仅是 NULLS.

val y = when($"x" === 0, $"x".cast("double")).when($"x" === 1, lit(null)).otherwise(lit("NaN").cast("double"))
val df = spark.range(0, 3).toDF("x").withColumn("y", y)

df.withColumn("y", when($"y".isNull, 0.0).otherwise($"y")).show()
df.na.fill(0.0, Seq("y")).show()