pyspark - PySpark - 无法按另一列的值过滤行
问题描述
我一直试图让这个简单的代码工作,但到目前为止还没有运气。
df = sqlContext.createDataFrame([
('www.example.com/researc', 'Research Reports'),
('www.example.com/careers', 'Careers'),
('www.example.com/blogs', 'blogs'),
('www.example.com', None),
('www.example.com/navigation', None),
('www.example.com', 'main'),
('www.example.jp', None),
('', 'blogs')], ['A', 'B'])
display(df.toPandas())
df.printSchema()
print('Original DF')
display(df.toPandas())
filter_mask = df.where(df['B'].isNotNull())
print("\n\nFilter Mask")
display(filter_mask.toPandas())
print('\n\nfilter_mask[A]')
filter_mask.select('A').show()
# Why is "response" returning everything?!
response = df.filter(df['A'].isin(filter_mask['A']))
print("\n\nResulting DF")
display(response.toPandas())
我试图从'B'中过滤掉所有空值,然后只保留A中的所有相应值;但是我的逻辑继续返回整个数据框。有人可以帮我弄清楚为什么我没有正确过滤这些值吗?
更新
所以我想要的输出是:
+--------------------+----------------+
| A| B|
+--------------------+----------------+
|www.example.com/r...|Research Reports|
|www.example.com/c...| Careers|
|www.example.com/b...| blogs|
| www.example.com| null|
| www.example.com| main|
| | blogs|
+--------------------+----------------+
^ 基本上,我想保留所有映射到 B 中任何非空值的 URL。(因此对于“www.example.com”,即使其中一个映射为空,因为它的另一个实例没有映射为null;它仍然在最终结果中返回。
解决方案
df.filter(df.B.isNotNull()).show()
推荐阅读
- python - Apache Avro - 将序列化数据写入流式传输
- python - 在将 Google Maps API 与 Python 结合使用时,distance_matrix 是否需要任何特定的导入命令?
- swift - 用户文档目录的 URL 为空
- mule - Mule esb:“滚动”日志文件是默认行为吗?
- bash - 在运行 bash 脚本的一个阶段失败后,Jenkins 管道未执行下一阶段
- java - 为什么我在 Ubuntu 上收到证书错误,但在 Windows 上却没有?
- windows-10 - 如何在 WSL2 上设置工作 X11 转发
- exchange-server - OfficeJs getUserIdentityTokenAsync 无法生成令牌
- sql - EF Core 和 SQL Server - 没有重叠的日期范围
- java - 没有静态资源 Spring Boot 的映射