Spark"用0替换null"性能比较

use*_*641 8 apache-spark spark-dataframe

Spark 1.6.1,Scala api.

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

myDF.withColumn("pipConfidence", when($"mycol".isNull, 0).otherwise($"mycol"))
Run Code Online (Sandbox Code Playgroud)

2.

myDF.na.fill(0, Seq("mycol"))
Run Code Online (Sandbox Code Playgroud)

它们基本相同还是一种方式首选?

谢谢!

小智 12

有不一样但性能应该相似.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()
Run Code Online (Sandbox Code Playgroud)