Spark SQL:若单词列表中的单词包含在列中,则在新列中返回找到的单词

2024-09-28 23:03:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从单词列表中返回一列新的“已找到”单词。我已经编写了代码,当找到单词时返回“T”。如何返回实际单词

我的单字:

[“谋杀”, "暴力",, “闪回”, "邪教",, “复仇”, “浪漫”, “喜剧”, “迷幻药”, "悬念",, “火车”]

目前:

from pyspark.sql.functions import col, when

df.select(
    "*", 
    when(col("tags").rlike("|".join(list)), 'T').alias("toptags"), 
).show()

+--------------------+-------+
|                tags|toptags|
+--------------------+-------+
|cult, horror, got...|      T|
|            violence|      T|
|they dream of som...|   null|
|                imdb|   null|
|inspiring, romant...|      T|
+--------------------+-------+

预期结果:

+--------------------+-------+
|                tags|toptags|
+--------------------+-------+
|cult, horror, got...|   cult |
|            violence|violence|
|they dream of som...|   null |
|                imdb|   null |
|inspiring, romant...|romance |
+--------------------+-------+

Tags: oftagscol单词nullimdbwhengot