java - 多列工作错误的 Spark 过滤器
问题描述
我有一个包含 30 行和 10 列的数据框。列名是动态变化的。filter
此数据框在 2 行的 8 列中有空值,我正在尝试使用带有运算符的 sparkSQL 删除这些记录and
。
我看到过滤器的奇怪行为。当我用
df.filter('(`1Heavy Buyers` is null) and (`2Non Buyers` is null) and (`3Total Buyers` is null) and (`4Heavy Buyers` is null) and (`7Non Buyers` is null) and (`5Total Buyers` is null) and (`6Heavy Buyers` is null) and (`8Non Buyers` is null) and (`9otal Buyers` is null)')
我找回了 2 条正确的记录。
但是当尝试使用非空行时
df.filter('(`1Heavy Buyers` is not null) and (`2Non Buyers` is not null) and (`3Total Buyers` is not null) and (`4Heavy Buyers` is not null) and (`7Non Buyers` is not null) and (`5Total Buyers` is not null) and (`6Heavy Buyers` is not null) and (`8Non Buyers` is not null) and (`9otal Buyers` is not null)')
我只取回了 16 条记录,应该是 28 条。在这种情况下,即使一列具有空值而不是提到的所有列,过滤器也会删除。
我正在使用火花 2.3.0。
我不明白我在这里做错了什么。
解决方案
您运行的第一个查询意味着您打算将所有记录保留在数据框中,其中提到的所有六列的值为空。
但是,在第二个查询中,将保留所有提及的列的值不为空的所有记录。这意味着,即使只有一列具有空值,它也会被过滤掉。要获得您需要的结果,您可以运行以下查询:
df.filter('(`1Heavy Buyers` is not null) or (`2Non Buyers` is not null) or (`3Total Buyers` is not null) or (`4Heavy Buyers` is not null) or (`7Non Buyers` is not null) or (`5Total Buyers` is not null) or (`6Heavy Buyers` is not null) or (`8Non Buyers` is not null) or (`9otal Buyers` is not null)')
上面的查询将为您提供所需的 28 计数。如果您有任何疑问,请随时发表评论。
推荐阅读
- javascript - 如何使用 Fetch 添加动态 URL?
- javascript - 显示带有下标(或下标)样式的菜单项标签
- java - 在被引用为匿名内部类的替代品的视频中看到了这个,它是什么?
- python - 从python中的多个列表中随机选择
- angular - Jhipster 启动非常慢 - 启动空白引导应用程序需要 2 小时
- python - 使用 Pandas(Python) 从 Google Drive 读取非常大的 csv 文件
- sql - 如何在sql中找到count的最大值和最小值
- javascript - 如何在 Selenium Webdriver Javascript 中保存为网页?
- flutter - Flutter - 如何在多个地方重用一些代码
- elasticsearch - 如何在弹性搜索中匹配两个索引之间的数据