【问题标题】:How to combine and collect elements of an RDD into a list in pyspark如何将 RDD 的元素组合并收集到 pyspark 中的列表中
【发布时间】:2017-07-04 15:50:16
【问题描述】:

我正在使用 Apache Spark for python 并创建了一个 spark 数据框,其中包含名称、纬度、经度作为列名。

我的 RDD 数据框格式为:

name     latitude      longitude

M          1.3           22.5
S          1.6           22.9
H          1.7           23.4
W          1.4           23.3
C          1.1           21.2
...        ...           ....

我知道只收集我能做的纬度

list_of_lat = df.rdd.map(lambda r: r.latitude).collect()

print list_of_lat

[1.3,1.6,1.7,1.4,1.1,...]

但是,我需要将纬度和经度值一起收集在一个列表中,格式为:

[[1.3,22.5],[1.6,22.9],[1.7,23.4]...]

我试过了

lat_lon = df.rdd.map(lambda r,x : r.latitude, x.longitude).collect()

但是这不起作用。

我需要使用 spark,因为它是一个非常大的数据集(~1M 行)。

任何帮助将不胜感激。谢谢

【问题讨论】:

  • 你试过lat_lon = df.rdd.map(lambda x : [x.latitude, x.longitude]).collect()吗?
  • 非常感谢 - 这行得通!

标签: python pyspark spark-dataframe pyspark-sql


【解决方案1】:

我假设lat_lon = df.rdd.map(lambda r,x : r.latitude, x.longitude).collect()

给你以下错误 NameError: name 'x' is not defined

试试

lat_lon = df.rdd.map(lambda x : [x.latitude, x.longitude]).collect()

【讨论】:

  • 谢谢你的作品!这正是它给出的错误 - 很抱歉在我的原始帖子中省略了这一点。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-25
  • 1970-01-01
相关资源
最近更新 更多