根据列表值更新列Dataframe列

时间:2018-11-14 02:11:12

标签: python-3.x apache-spark pyspark apache-spark-sql

我有一个spark数据帧列idarticles,还有3个列表,如下所示。a_listb_listc_list

a_list=[4, 10], b_list=[11,3], c_list=[3,6]

df = spark.createDataFrame([(1, 4), (2, 3), (5, 6)], ("id", "articles"))

我想根据数据框列Found的值与列表articles之间的匹配来更新列(a_list, b_list,c_list)

当前,我只能在有2个列表的情况下执行以下代码。

import pyspark.sql.functions as func                    
df.withColumn('E', func.when(df.articles.isin(a_list), 'Found in a_list').otherwise('Found in b_list'))            

如何推断两个以上的列表?

预期输出

+---+--------+---------------+                                                  
| id|articles|          Found|
+---+--------+---------------+
|  1|       4|Found in a_list|
|  2|       3|Found in b_list|
|  5|       6|Found in c_list|
+---+--------+---------------+

0 个答案:

没有答案
相关问题