I have dataframe as below
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 |
I would like to split to dict of dataframe group by some column by using pyspark as below
{ "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 | }
I'm using spark 2.7.1 with pyspark.
I alredy try
out = dict()
for i in ["Key1", "Key2","Key3"]:
out[i] = df.where(df.key == i)
return out
But i'm looking for other faster way