I have a dataframe as shown below
+----------+----+----+----+
| date|col1|col2|col3|
+----------+----+----+----+
|2021-05-01| 20| 30| 40|
|2021-05-02| 200| 300| 10|
+----------+----+----+----+
I wish to pivot/transpose this dataframe as
+-----+----------+----------+
|col |2021-05-01|2021-05-02|
+-----+----------+----------+
|Col1 | 20| 200|
|Col1 | 30| 300|
|Col1 | 40| 10|
+-----+----------+----------+
Other stackoverflow articles like this and this helped me to some extent but I have been able to reach to a solution.
My approaches were (all failed attempts)
scala> dUnion.groupBy("date").pivot("date").agg(first("col1")).show()
+----------+----------+----------+
| date|2021-05-01|2021-05-02|
+----------+----------+----------+
|2021-05-02| null| 200|
|2021-05-01| 20| null|
+----------+----------+----------+
scala> dUnion.groupBy("date", "col1", "col2", "col3").pivot("date").agg(first("col1")).show()
+----------+----+----+----+----------+----------+
| date|col1|col2|col3|2021-05-01|2021-05-02|
+----------+----+----+----+----------+----------+
|2021-05-02| 200| 300| 10| null| 200|
|2021-05-01| 20| 30| 40| 20| null|
+----------+----+----+----+----------+----------+
But the closet I could come up with was
scala> dUnion.groupBy().pivot("date").agg(first("col1")).show()
+----------+----------+
|2021-05-01|2021-05-02|
+----------+----------+
| 20| 200|
+----------+----------+