在Spark DataFrame中替换空值 [英] Replace null value in Spark DataFrame

查看:2271
本文介绍了在Spark DataFrame中替换空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里看到一个解决方案,但是当我尝试它对我来说不行。
首先我导入一个cars.csv文件

I saw a solution here but when I tried it doesn't work for me. First I import a cars.csv file

scala> val df = sqlContext.read
                          .format("com.databricks.spark.csv").option("header", "true")
                          .load("/usr/local/spark/cars.csv")

看起来像这样

+----+-----+-----+--------------------+-----+
|year| make|model|             comment|blank|
+----+-----+-----+--------------------+-----+
|2012|Tesla|    S|          No comment|     |
|1997| Ford| E350|Go get one now th...|     |
|2015|Chevy| Volt|                null| null|

然后我这样做

df.na.fill("e",Seq("blank"))

但空白中的空值没有改变。任何人都可以帮助我感谢

But the null in blank didn't change. Can anyone help me thanks

推荐答案

这基本上很简单。您需要创建一个新的 DataFrame 。我正在使用您之前定义的 DataFrame df

This is basically very simple. You'll need to create a new DataFrame. I'm using the DataFrame df that you have defined earlier.

val newDf = df.na.fill("e",Seq("blank"))

DataFrame 是不可变结构。
每次执行需要存储的转换时,都需要将变换后的 DataFrame 影响到一个新值。

DataFrames are immutable structures. Each time you perform a transformation which you need to store, you'll need to affect the transformed DataFrame to a new value.

这篇关于在Spark DataFrame中替换空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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