My DataFrame has the following structure:
df = spark.createDataFrame(
[('B', 'a', 10),
('B', 'b', 20),
('C', 'c', 30)],
['Brand', 'Type', 'Amount'])
df.show()
# +-----+----+------+
# |Brand|Type|Amount|
# +-----+----+------+
# | B| a| 10|
# | B| b| 20|
# | C| c| 30|
# +-----+----+------+
I want to reduce the amount of rows by grouping type and amount into one single column of type: Map.
So Brand will be unique and MAP_type_AMOUNT will have key,value for each type amount combination.
I think, spark.sql might have some functions to do it, or do I have to use RDD and make my "own" conversion to map type?
Expected output:
---------------------------
| Brand | MAP_type_AMOUNT |
---------------------------
| B | {a: 10, b:20} |
| C | {c: 30} |
---------------------------