Spark“用0替换null”性能比较

问题描述 投票:7回答:1

Spark 1.6.1,api规模。

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

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

2.

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

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

谢谢!

apache-spark spark-dataframe
1个回答
11
投票

有不一样但性能应该相似。 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()
© www.soinside.com 2019 - 2024. All rights reserved.