如何使用过滤器从scala的数据框中获取包含空值的行集

雷卡·普加里(Rekha Pujari)

我是新手,有一个关于基于空条件过滤数据帧的问题。我已经经历了许多答案,其中包括解决方案

df.filter(($"col2".isNotNULL) || ($"col2" !== "NULL")  || ($"col2" !== "null")  || ($"col2".trim !== "NULL"))

但就我而言,由于架构不固定,我无法编写硬编码的列名。我正在读取csv文件,并且根据其中的列,我必须过滤数据框以获取空值,并希望在另一个数据框中使用它。简而言之,任何具有空值的列,该完整行都应位于不同的数据框下。

例如:Input DataFrame:

+----+----+---------+---------+
|name|  id|    email|  company|
+----+----+---------+---------+
|  n1|null|[email protected]|[c1,1,d1]|
|  n2|   2|null     |[c1,1,d1]|
|  n3|   3|[email protected]| null    |
|  n4|   4|[email protected]|[c2,2,d2]|
|  n6|   6|[email protected]|[c2,2,d2]|

输出:

+----+----+---------+---------+
|name|  id|    email|  company|
+----+----+---------+---------+
|  n1|null|[email protected]|[c1,1,d1]|
|  n2|   2|null     |[c1,1,d1]|
|  n3|   3|[email protected]| null    |

先感谢您。

呼叫

为了处理空值和数据帧,spark具有一些有用的功能。

我将显示一些具有不同列数的数据框示例。

      val schema = StructType(List(StructField("id", IntegerType, true), StructField("obj",DoubleType, true)))
      val schema1 = StructType(List(StructField("id", IntegerType, true), StructField("obj",StringType, true), StructField("obj",IntegerType, true)))

      val t1 = sc.parallelize(Seq((1,null),(1,1.0),(8,3.0),(2,null),(3,1.4),(3,2.5),(null,3.7))).map(t => Row(t._1,t._2))
      val t2 = sc.parallelize(Seq((1,"A",null),(2,"B",null),(3,"C",36),(null,"D",15),(5,"E",25),(6,null,7),(7,"G",null))).map(t => Row(t._1,t._2,t._3))
      val tt1 = spark.createDataFrame(t1, schema)
      val tt2 = spark.createDataFrame(t2, schema1)

      tt1.show()
      tt2.show()

      // To clean all rows with null values
      val dfWithoutNull = tt1.na.drop()
      dfWithoutNull.show()

      val df2WithoutNull = tt2.na.drop()
      df2WithoutNull.show()

      // To fill null values with another value
      val df1 = tt1.na.fill(-1)
      df1.show()

      // to get new dataframes with the null values rows
      val nullValues = tt1.filter(row => row.anyNull == true)
      nullValues.show()

      val nullValues2 = tt2.filter(row => row.anyNull == true)
      nullValues2.show()

输出

// input dataframes
+----+----+
|  id| obj|
+----+----+
|   1|null|
|   1| 1.0|
|   8| 3.0|
|   2|null|
|   3| 1.4|
|   3| 2.5|
|null| 3.7|
+----+----+

+----+----+----+
|  id| obj| obj|
+----+----+----+
|   1|   A|null|
|   2|   B|null|
|   3|   C|  36|
|null|   D|  15|
|   5|   E|  25|
|   6|null|   7|
|   7|   G|null|
+----+----+----+

// Dataframes without null values
+---+---+
| id|obj|
+---+---+
|  1|1.0|
|  8|3.0|
|  3|1.4|
|  3|2.5|
+---+---+

+---+---+---+
| id|obj|obj|
+---+---+---+
|  3|  C| 36|
|  5|  E| 25|
+---+---+---+

// Dataframe with null values replaced
+---+----+
| id| obj|
+---+----+
|  1|-1.0|
|  1| 1.0|
|  8| 3.0|
|  2|-1.0|
|  3| 1.4|
|  3| 2.5|
| -1| 3.7|
+---+----+

// Dataframes which the rows have at least one null value
+----+----+
|  id| obj|
+----+----+
|   1|null|
|   2|null|
|null| 3.7|
+----+----+

+----+----+----+
|  id| obj| obj|
+----+----+----+
|   1|   A|null|
|   2|   B|null|
|null|   D|  15|
|   6|null|   7|
|   7|   G|null|
+----+----+----+

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

列列表中的数据框过滤器空值

使用过滤器和正则表达式根据 Pandas 数据框中另一个变量的部分文本替换空值

在Pandas数据框中使用过滤器过滤行

如何使用AngularJS应用过滤器以获取JSON数组中的JSON对象中具有相同值的元素

使用过滤器获取数据对象

在SparkSQL中对数据集使用限制后应用过滤器时,结果奇怪

将JSON数据集加载到Spark中,然后使用过滤器,地图等

根据另一个数据框中的列值在数据框中找到一行,并对其应用过滤器

如何在使用过滤器和包含在柏树中时使用变量

如何在 google sheet API 上使用过滤器来显示 ASC 中的数据值?

Django,如何使用过滤器检查参数中是否包含字符串字段

在javascript中使用过滤器方法时从数组中获取所有值

使用过滤器返回对象中的属性值

使用过滤器后如何组织数据

如何在Scala中链接使用过滤器生成元组的地图?

如何使用过滤器从列表中查询?

如何使用过滤器从ouchdb中删除文档?

如何使用过滤器javascript删除数组值中的元素

在Vue2中使用过滤器时如何重置子组件的值

R使用过滤器和管道过滤以日期为条件的大型数据框

根据数据框熊猫中的列过滤器获取n行

在PostgreSQL中查询对月份和年份应用过滤器的数据集

使用purrr根据其他数据集应用过滤器和变异

使用过滤器输入显示数据帧表以过滤行

Postgres在使用过滤器时忽略空值而不是数组

迭代数组并使用过滤器检查对象并跳过空值

使用过滤器计算第二个数据帧中某个值的出现

使用过滤器将数据框应用于不同变量

在分组数据框中使用过滤器和样本