apache-spark - 如何在pyspark中做多个array_union和array_intersection
问题描述
假设我有一个 pyspark 数据框,其中包含以下列:数组类型的 c1、c2、c3、c4 和 c5。现在如果我想做:(c1)交集(c2 union c3)交集(c2 union c4 union c5)
我可以在循环中的两列上使用 array_union 并在 withColumn 的帮助下继续添加一列,然后类似地进行一轮交叉。
如何在 PySpark 中有效地做到这一点?有没有更聪明的方法来做到这一点?
解决方案
你不需要使用循环,你可以有一个大withColumn
语句。这也应该是最快的选择,因为您只使用 spark 内置函数。
from pyspark.sql import functions as f
df = df.withColumn("crazy_set_operation",
f.array_intersect(f.col("c1"),
f.array_intersect(
f.array_union(f.col("c2"), f.col("c3")),
f.array_union(f.array_union(f.col("c2"), F.col("c4")), f.col("c5"))
)
)
)
推荐阅读
- amazon-web-services - 是否可以在云平台部署 HBase 组件
- postgresql - 为什么 Testcontainers 找不到使用 init 脚本创建的表?
- vim - 我很难将 Vim 在线编辑器与 Vimium 一起使用
- python - 防止 Django 的单元测试覆盖模型克隆上的变量
- python - 如何让 Try except 输出更多细节?显示它到底发生在哪里?- Python
- node.js - Redis 是否重置过期时间
- javascript - 是否可以在发布到服务器之前拆分 zip 文件?
- ubuntu - snmpwalk:-c:未知对象标识符(未找到子 ID:(顶部)-> -c)
- java - 通过java流收集到hashMap的多个组
- java - 为什么 onClick() 函数需要参数而不是参数