I have the following data in spark 2.4.5:
data = [
('1234', '203957', '2010', 'London', 'CHEM'),
('1234', '203957', '2010', 'London', 'BIOL'),
('1234', '288400', '2012', 'Berlin', 'MATH'),
('1234', '288400', '2012', 'Berlin', 'CHEM'),
]
d = spark.createDataFrame(data, ['auid', 'eid', 'year', 'city', 'subject'])
d.show()
+----+------+----+------+-------+
|auid| eid|year| city|subject|
+----+------+----+------+-------+
|1234|203957|2010|London| CHEM|
|1234|203957|2010|London| BIOL|
|1234|288400|2012|Berlin| MATH|
|1234|288400|2012|Berlin| CHEM|
+----+------+----+------+-------+
from which I need to get df grouped by auid, with a chronological order of cities, i.e. London, Berlin and [[Berlin, 2010], [London, 2012]] in another column, plus I need sorted by descending frequency column with subjects: [CHEM,2], [BIOL, 1], [MATH, 1]. Or it could be just like [CHEM, BIOL, MATH].
I tried this:
d.groupBy('auid').agg(func.collect_set(func.struct('city', 'year')).alias('city_set')).show(10, False)
which leads to this:
+----+--------------------------------+
|auid|city_set |
+----+--------------------------------+
|1234|[[Berlin, 2012], [London, 2010]]|
+----+--------------------------------+
Here I am stuck and need help. (would appreciate a hint on to sort values in city_set)