火花“用0替换null".性能比较 [英] Spark "replacing null with 0" performance comparison

查看:105
本文介绍了火花“用0替换null".性能比较的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spark 1.6.1,Scala API.

Spark 1.6.1, Scala api.

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

For a dataframe, I need to replace all null value of a certain column with 0. I have 2 ways to do this. 1.

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

2.

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

是基本上相同还是首选?

Are they essentially the same or one way is preferred?

谢谢!

推荐答案

虽然不一样,但性能应该相似. na.fill使用coalesce,但它不仅替换NULLS,而且替换了NaNNULLs.

There are not the same but performance should be similar. na.fill uses coalesce but it replaces NaN and NULLs not only 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()

这篇关于火花“用0替换null".性能比较的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆