【发布时间】:2021-03-29 10:21:50
【问题描述】:
我有一个 pyspark 数据帧如下:
import pyspark.sql.functions as F
import pyspark.sql.types as T
from pyspark.sql.functions import udf
schema = T.StructType([ # schema
T.StructField("id", T.StringType(), True),
T.StructField("code", T.ArrayType(T.StringType()), True)])
df = spark.createDataFrame([{"id": "1", "code": ["a1", "a2","a3","a4"]},
{"id": "2", "code": ["b1","b2"]},
{"id": "3", "code": ["c1","c2","c3"]},
{"id": "4", "code": ["d1", "b3"]}],
schema=schema)
给出输出
df.show()
| id| code|
|---|----------------|
| 1|[a1, a2, a3, a4]|
| 2| [b1, b2]|
| 3| [c1, c2, c3]|
| 4| [d1, b3]|
我希望能够通过向函数提供列和列表来过滤行,如果有任何相交则返回 true(使用与 here 的不相交,因为会有很多非命中)
def lst_intersect(data_lst,query_lst):
return not set(data_lst).isdisjoint(query_lst)
lst_intersect_udf = F.udf(lambda x,y: lst_intersect(x,y), T.BooleanType())
当我尝试应用它时
query_lst = ['a1','b3']
df = df.withColumn("code_found", lst_intersect_udf(F.col('code'),F.lit(query_lst)))
得到以下错误
Unsupported literal type class java.util.ArrayList [a1, b3]
我可以通过更改函数等来解决它 - 但想知道我在 F.lit(query_lst) 上做错了什么?
【问题讨论】:
标签: python pandas apache-spark pyspark apache-spark-sql