【发布时间】:2020-01-30 09:05:22
【问题描述】:
我是 Spark 的新手,我找不到解决问题的方法,非常感谢任何建议或帮助。
我有一个 Pyspark.sql.dataframe,其中包含两个数组列,其中包含字符串。两个列数组的长度不一致,某些行也有 Null 条目。我需要比较这两列,并且必须为 B 列中的每一行删除数组的一个元素,当它在 OVERRIDE 列的数组中的该行中找到时。
+---------------+---------------+
| OVERRIDE | B |
+---------------+---------------+
| ['a']| ['a','b']|
| null| ['b']|
| null| ['a','c']|
| ['d','g']| ['d','g']|
| null| null|
| ['f']| ['f']|
+---------------+---------------+
最后应该是这样的:
+---------------+---------------+
| OVERRIDE | B |
+---------------+---------------+
| ['a']| ['b']|
| null| ['b']|
| null| ['a','c']|
| ['d','g']| null|
| null| null|
| ['f']| null|
+---------------+---------------+
我试过了
from pyspark.sql.functions import array_remove, array_intersect
df = df.withColumn('B', array_remove(df.B, df.OVERRIDE))
还有
df = df.withColumn('B', array_remove(df.B, array_intersect(df.OVERRIDE, df.B)))
但了解到 array_remove() 不能遍历列,而是只能取一个元素(例如“a”)将其删除,然后在 B 列的所有行中。
我是否必须构建一个 udf 函数,如果是,我应该怎么做?
【问题讨论】:
标签: python pyspark apache-spark-sql pyspark-sql higher-order-functions