如何将 RDD 的元素组合并收集到 pyspark 中的列表中

How to combine and collect elements of an RDD into a list in pyspark

我正在为 python 使用 Apache Spark,并创建了一个以名称、纬度、经度作为列名的 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,因为它是一个非常大的数据集(~100 万行)。

如有任何帮助,我们将不胜感激。谢谢

我假设 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()