Merge rows from one pair of columns into another

Viewed 35
1 Answers

Correct me if I'm wrong, but I understand that you have a dataframe with 4 columns and you want two of them to be in the previous couple of columns right?

For instance with this input (only two rows for simplicity)

df.show
+----+----------+-----------+----------+---------+
|name|     date1|      cost1|     date2|    cost2|
+----+----------+-----------+----------+---------+
|   A|2013-03-25|19923245.06|          |         |
|   B|2015-06-04| 4104660.00|2017-10-16|392073.48|
+----+----------+-----------+----------+---------+

With just a couple of selects and a unionn you can achieve what you want

df.select("name", "date1", "cost1")
  .union(df.select("name", "date2", "cost2"))
  .withColumnRenamed("date1", "date")
  .withColumnRenamed("cost1", "cost")

+----+----------+-----------+
|name|      date|       cost|
+----+----------+-----------+
|   A|2013-03-25|19923245.06|
|   B|2015-06-04| 4104660.00|
|   A|          |           |
|   B|2017-10-16|  392073.48|
+----+----------+-----------+
Related