首页 > 解决方案 > Spark Column 将所有列表合并为 1 个单个列表

问题描述

我希望下面的列合并到一个用于 n-gram 计算的列表中。我不确定如何将列中的所有列表合并为一个。

+--------------------+
|              author|
+--------------------+
|       [Justin, Lee]|
|[Chatbots, were, ...|
|[Our, hopes, were...|
|[And, why, wouldn...|
|[At, the, Mobile,...|
+--------------------+

(编辑)更多信息:我希望将其作为 spark df 列和所有单词,包括单个列表中的重复单词。数据有点大,所以我想尽量避免收集等方法

标签: pyspark

解决方案


DataFrames,与其他分布式数据结构一样,不可迭代,只能使用专用的高阶函数和/或 SQL 方法来访问

假设您的数据框是 DF1,输出是 DF2

你需要类似的东西:

values = [(['Justin', 'Lee'],), (['Chatbots', 'were'],), (['Our', 'hopes', 'were'],),
          (['And', 'why', 'wouldn'],), (['At', 'the', 'Mobile'],)]
df = spark.createDataFrame(values, ['author', ])
df.agg(F.collect_list('author').alias('author')).show(truncate=False)

如果有效,请点赞


推荐阅读