I have a data frame looks like -
+---+---+---+---+
| id| w1| w2| w3|
+---+---+---+---+
| 1|100|150|200|
| 2|200|400|500|
| 3|500|600|150|
+---+---+---+---+
I want output looks like -
full total_amt
w1 800
w2 1150
w3 850
My code is -
df = spark.createDataFrame(
[(1, 100,150,200), (2, 200,400,500), (3, 500,600,150)], ("id", "w1","w2","w3"))
res = df.unionAll(
df.select([
F.lit('All').alias('id'),
F.sum(df.w1).alias('w1'),
F.sum(df.w2).alias('w2'),
F.sum(df.w3).alias('w3')
]))
res.show()
But output gives me -
+---+---+----+---+
| id| w1| w2| w3|
+---+---+----+---+
| 1|100| 150|200|
| 2|200| 400|500|
| 3|500| 600|150|
|All|800|1150|850|
+---+---+----+---+
I think after addition need to create pivot. All the fields are numeric in nature.