从数据框中过滤数据

时间:2021-03-24 17:44:14

标签: scala apache-spark apache-spark-sql

我正在尝试过滤一列为空的数据框中的数据。从源 json 文件中,我获得了列的值,如下所示:

col:null

该列的架构将变为字符串。

当我过滤行时,我仍然得到记录。以下命令均无效。不确定我在这里缺少什么。

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

数据帧输出低于

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

2 个答案:

答案 0 :(得分:1)

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

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

或在数据帧 API 中,

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

答案 1 :(得分:0)

//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")