如何在 Spark 中收集单个列? [英] How do I collect a single column in Spark?
本文介绍了如何在 Spark 中收集单个列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想对单个列执行操作.不幸的是,在我转换该列之后,它现在不再是它来自的数据帧的一部分,而是一个 Column 对象.因此,无法收集.
I would like to perform an action on a single column. Unfortunately, after I transform that column, it is now no longer a part of the dataframe it came from but a Column object. As such, it cannot be collected.
这是一个例子:
df = sqlContext.createDataFrame([Row(array=[1,2,3])])
df['array'].collect()
这会产生以下错误:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'Column' object is not callable
如何在单列上使用 collect()
函数?
How can I use the collect()
function on a single column?
推荐答案
Spark >= 2.0
从 Spark 2.0.0 开始,您需要明确指定 .rdd
才能使用 flatMap
Starting from Spark 2.0.0 you need to explicitly specify .rdd
in order to use flatMap
df.select("array").rdd.flatMap(lambda x: x).collect()
火花<2.0
只需select
和flatMap
:
df.select("array").flatMap(lambda x: x).collect()
## [[1, 2, 3]]
这篇关于如何在 Spark 中收集单个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文