从dataframe筛选数据

ct3nt3jp  于 2021-07-09  发布在  Spark
关注(0)|答案(2)|浏览(270)

我试图过滤Dataframe中一列为空的数据。从源json文件中,我得到列的值,如下所示:

col:null

该列的架构将变成字符串。
当我过滤行时,我仍然得到记录。以下命令均无效。不知道我在这里错过了什么。

df.filter($"col" =!= "null")
df.filter($"col" =!= lit("null"))

Dataframe输出低于

+----------+-------------------+
|RecordCnt |col                |
+----------+-------------------+
|    500000|               null|
+----------+-------------------+
r8xiu3jd

r8xiu3jd1#

//if you want to apply as where clause you can do it as .

val newDF = df.where(df("col").isNotNull)

OR

// if you want to filter as 
val newDF = df.filter($"col".isNotNull)

OR 

val newDF =df.filter("col is not null")
3bygqnnd

3bygqnnd2#

不能使用相等运算符与null进行比较。你需要使用 is not null ,例如。

val df2 = df.filter("col is not null")

或者在Dataframeapi中,

val df2 = df.filter($"col".isNotNull)

相关问题