我的目标是如果可能的话,将列值作为 pyspark 数据帧中的列表获取到变量中。
Expected output = ["a", "b", "c", ... ]
我试过了:
[
col.__getitem__("x")
for col in data.select("x").collect()
]
但它给出了
Row
对象的列表。
Output : [Row(x='a'), Row(x='b'), Row(x='c'), ...]
我不想使用collect也不需要Row对象。
尝试了另一种方法:
data.select(f.collect_list("x")).collect()
比早期版本稍好一些,但得到:
Output = [Row(collect_list(x) = ['a', 'b', 'c', ...]]
提前致谢,新年快乐!
尝试了三种不同的解决方案:
df.select(f.collect_list("x").alias("temp")).first()["temp"]
Time taken : 32.43s
df.select("x").rdd.flatMap(lambda x:x).collect()
Time taken : 13.19s
[col.__getitem__("x") for col in df.select("x").collect()]
Time taken : 22.77s
尽管我使用的是collect,但它比其他解决方案更快。 PS
df.count ~ 116M