首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何合并PySpark数据中的单词列表?

如何合并PySpark数据中的单词列表?
EN

Stack Overflow用户
提问于 2022-07-26 07:28:06
回答 1查看 38关注 0票数 0

我有一个包含单词列表的数据,我需要将它们合并成一个句子。

Dataframe:

代码语言:javascript
复制
temp = spark.createDataFrame([
    (0, ['Julia', 'is', 'awesome']),
    (2, ['Data-science', 'is','cool']),
    (3, ['Machine','learning'])
], ["id", "words"])

# +---+------------------------+
# |id |words                   |
# +---+------------------------+
# |0  |[Julia, is, awesome]    |
# |2  |[Data-science, is, cool]|
# |3  |[Machine, learning]     |
# +---+------------------------+

temp.printSchema()
# root
#  |-- id: long (nullable = true)
#  |-- words: array (nullable = true)
#  |    |-- element: string (containsNull = true)

我在申请rdd。

代码语言:javascript
复制
rdd_df = temp.rdd.map(lambda x: [x['id'], ' '.join(x['words'])])
spark.createDataFrame(rdd_df, temp.schema).show(10, False)

# +---+---------------------------------------------------------+
# |id |words                                                    |
# +---+---------------------------------------------------------+
# |0  |[ ' J u l i a ' ,   ' i s ' ,   ' a w e s o m e ' ]      |
# |2  |[ ' D a t a - s c i e n c e ' ,   ' i s ' , ' c o o l ' ]|
# |3  |[ ' M a c h i n e ' , ' l e a r n i n g ' ]              |
# +---+---------------------------------------------------------+

但是上面的代码没有返回所需的输出。在不使用RDD的情况下,我们还可以应用其他解决方案吗?

期望输出:

代码语言:javascript
复制
+---+--------------------+
|id |words               |
+---+--------------------+
|0  |Julia is awesome    |
|1  |Data-science is cool|
|2  |Machine             |
+---+--------------------+
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-07-26 07:44:49

如果您有一个单词列表(一个字符串数组),则可以使用array_join组合它们。

代码语言:javascript
复制
from pyspark.sql import functions as F
temp = spark.createDataFrame([
    (0, ['Julia', 'is', 'awesome']),
    (1, ['Data-science', 'is','cool']),
    (2, ['Machine','learning'])
], ["id", "words"])

temp = temp.withColumn('words', F.array_join('words', ' '))

temp.show()
# +---+--------------------+
# | id|               words|
# +---+--------------------+
# |  0|    Julia is awesome|
# |  1|Data-science is cool|
# |  2|    Machine learning|
# +---+--------------------+
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73119302

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档