如何在Spark中收集单个列? [英] How do I collect a single column in Spark?

查看:115
本文介绍了如何在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?

推荐答案

火花> = 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

只需selectflatMap:

df.select("array").flatMap(lambda x: x).collect()
## [[1, 2, 3]] 

这篇关于如何在Spark中收集单个列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆