![](/img/trans.png)
[英]How to filter out rows from spark dataframe containing unreadable characters
[英]How to delete/filter the specific rows from a spark dataframe
这是解决方案。 根据您的数据集,我提出了问题-
dataframe 下面的条目不正确。 我想删除所有不正确的记录并只保留正确的记录 -
val Friends = Seq(
("Rahul", "99", "AA"),
("Rahul", "20", "BB"),
("Rahul", "30", "BB"),
("Mahesh", "55", "CC"),
("Mahesh", "88", "DD"),
("Mahesh", "44", "FF"),
("Ramu", "30", "FF"),
("Gaurav", "99", "PP"),
("Gaurav", "20", "HH")).toDF("Name", "Age", "City")
Arrays 用于滤波 -
val Name = List("Rahul", "Mahesh", "Gaurav")
val IncorrectAge = List(20, 55)
数据操作 -
Friends.filter(!(col("Name").isin(Name: _*) && col("Age").isin(IncorrectAge: _*))).show
这是 output -
+------+---+----+
| Name|Age|City|
+------+---+----+
| Rahul| 99| AA|
| Rahul| 30| BB|
|Mahesh| 88| DD|
|Mahesh| 44| FF|
| Ramu| 30| FF|
|Gaurav| 99| PP|
+------+---+----+
您也可以在连接的帮助下做到这一点..
创建不良记录 df -
val badrecords = Friends.filter(col("Name").isin(Name: _*) && col("Age").isin(IncorrectAge: _*))
用户 left_anti 加入 select 好友减坏记录 -
Friends.alias("left").join(badrecords.alias("right"), Seq("Name", "Age"), "left_anti").show
这是 output -
+------+---+----+
| Name|Age|City|
+------+---+----+
| Rahul| 99| AA|
| Rahul| 30| BB|
|Mahesh| 88| DD|
|Mahesh| 44| FF|
| Ramu| 30| FF|
|Gaurav| 99| PP|
+------+---+----+
我认为您可能想要翻转 not 条件.... dataframe 中的过滤器是 sql 中 where 子句的别名。
所以你希望查询是
df.filter(col("Name").isin(Name:_*) && col("Age").isin(Age:_*))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.