pyspark - Spark Column 将所有列表合并为 1 个单个列表
问题描述
我希望下面的列合并到一个用于 n-gram 计算的列表中。我不确定如何将列中的所有列表合并为一个。
+--------------------+
| author|
+--------------------+
| [Justin, Lee]|
|[Chatbots, were, ...|
|[Our, hopes, were...|
|[And, why, wouldn...|
|[At, the, Mobile,...|
+--------------------+
(编辑)更多信息:我希望将其作为 spark df 列和所有单词,包括单个列表中的重复单词。数据有点大,所以我想尽量避免收集等方法
解决方案
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)
如果有效,请点赞
推荐阅读
- python - 在 django 中嵌入 jupyter notebook
- syntax - 我如何代表丢失的 URN?
- excel - 使用偏移量时覆盖单元格的值
- python - 需要从mysql数据库中的多个表中提取数百万行
- python - 在 python 中绘制 3d 数据的 2d 切片(最好使用 matplotlib)
- nlp - 提取序列号和未知词的最佳NER算法
- android - 当使用动态链接安装应用程序时,为什么“继续”按钮不显示在 Play 商店中?
- json-server - 如何在发布请求时从 json-server 返回值?
- r - 使用 dplyr 和 forcats 包根据分组变量中的值更改因子水平
- java - 检查对请求的响应是否成功的最简单方法是什么?