【发布时间】:2020-06-11 05:46:44
【问题描述】:
我有如下数据框
id | Key | Value |
-----------------------
0 | Key1 | 100 |
1 | Key1 | 101 |
2 | Key1 | 102 |
3 | Key1 | 103 |
4 | Key2 | 104 |
5 | Key2 | 105 |
6 | Key2 | 106 |
7 | Key3 | 107 |
8 | Key3 | 108 |
9 | Key3 | 109 |
我想使用下面的 pyspark 将数据帧组的 dict 拆分为某列
{ "Key1" : id | Key | Value |
-----------------------
0 | Key1 | 100 |
1 | Key1 | 101 |
2 | Key1 | 102 |
3 | Key1 | 103 |,
"Key2" : id | Key | Value |
-----------------------
4 | Key2 | 104 |
5 | Key2 | 105 |
6 | Key2 | 106 |,
"Key3" : id | Key | Value |
-----------------------
7 | Key3 | 107 |
8 | Key3 | 108 |
9 | Key3 | 109 | }
我正在使用带有 pyspark 的 spark 2.7.1。
我已经试过了
out = dict()
for i in ["Key1", "Key2","Key3"]:
out[i] = df.where(df.key == i)
return out
但我正在寻找其他更快的方法
【问题讨论】:
-
在 pandas 中你可以使用
{k:v for k,v in df.groupby('Key')}也许它在 pyspark 中是一样的 -
您想要实现的是 {key,dataframe} 的 dict,这不是 spark 的最佳策略?