【发布时间】:2016-11-07 16:59:54
【问题描述】:
我想从包含单词列表的 DataFrame 转换为每个单词在其自己的行中的 DataFrame。
如何在 DataFrame 中的列上展开?
这是一个示例,其中包含我的一些尝试,您可以取消注释每个代码行并获取以下注释中列出的错误。我在 Python 2.7 和 Spark 1.6.1 中使用 PySpark。
from pyspark.sql.functions import split, explode
DF = sqlContext.createDataFrame([('cat \n\n elephant rat \n rat cat', )], ['word'])
print 'Dataset:'
DF.show()
print '\n\n Trying to do explode: \n'
DFsplit_explode = (
DF
.select(split(DF['word'], ' '))
# .select(explode(DF['word'])) # AnalysisException: u"cannot resolve 'explode(word)' due to data type mismatch: input to function explode should be array or map type, not StringType;"
# .map(explode) # AttributeError: 'PipelinedRDD' object has no attribute 'show'
# .explode() # AttributeError: 'DataFrame' object has no attribute 'explode'
).show()
# Trying without split
print '\n\n Only explode: \n'
DFsplit_explode = (
DF
.select(explode(DF['word'])) # AnalysisException: u"cannot resolve 'explode(word)' due to data type mismatch: input to function explode should be array or map type, not StringType;"
).show()
请指教
【问题讨论】:
-
更正 - 建议*
标签: python apache-spark pyspark apache-spark-sql