apache-spark - 如何在 Spark 中收集单个列?

标签 apache-spark dataframe pyspark apache-spark-sql

我想对单个列执行操作。
不幸的是,在我转换该列之后,它现在不再是它来自的数据帧的一部分,而是一个 Column 对象。因此,它无法收集。

下面是一个例子:

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()在单列上运行?

最佳答案

Spark >= 2.0

从 Spark 2.0.0 开始,您需要明确指定 .rdd为了使用flatMap

df.select("array").rdd.flatMap(lambda x: x).collect()

Spark < 2.0

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

关于apache-spark - 如何在 Spark 中收集单个列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35495197/

相关文章:

arrays - 对 Pyspark 中收集的列表执行线性回归

apache-spark - Kubernetes 上 Apache Spark 结构化流上的长时间 GC 暂停

hadoop - hbase 跳过区域服务器直接从 hfile 读取行

python - 如何将 xgboost 集成到 Spark 中? (Python)

r - 将预测的时间序列与 R 中的原始序列重叠

python - 如何使用 pyarrow 和 parquet 保存具有自定义类型的 Pandas DataFrame

sqlite - 通过 JDBC 从 Pyspark 写入 sqlite3 数据库时没有这样的表

apache-spark - 使用 Parquet 格式附加 Apache Spark 中列的描述

python - 如何绘制非数字列的直方图 w.r.t pandas 数据框中的另一个计算列?

python - 从 Spark DataFrame 选择空数组值