I have a pyspark dataframe like this,
+---+-----+
| id| info|
+---+-----+
| 1|--XX-|
| 2|XX--X|
+---+-----+
info column basically encodes the information in each month starting from 2018-01.
I would like to split the dataframe based on each item and created a melted version of it with each month added as a row.
The expected data looks like this,
+---+-------+----+
| id|monthid|info|
+---+-------+----+
| 1| 201801| -|
| 1| 201802| -|
| 1| 201803| X|
| 1| 201804| X|
| 1| 201805| -|
| 2| 201801| X|
| 2| 201802| X|
| 2| 201803| -|
| 2| 201804| -|
| 2| 201805| X|
+---+-------+----+
My data is huge and I want to do this avoiding any loops. I could think of a way to do this in PySpark easily.
With the wonderful solution from @wwnde, I'm able to generate the results for the example data I shared about. But things get tricky when the data info is more than 12 items long.
See the sample data and expected results below,
+---+-------------+
| id| info|
+---+-------------+
| 1|--XX---------|
| 2| XX--X|
+---+-------------+
Expected Result,
+---+-------+----+
| id|monthid|info|
+---+-------+----+
| 1| 201801| -|
| 1| 201802| -|
| 1| 201803| X|
| 1| 201804| X|
| 1| 201805| -|
| 1| 201806| -|
| 1| 201807| -|
| 1| 201808| -|
| 1| 201809| -|
| 1| 201810| -|
| 1| 201811| -|
| 1| 201812| -|
| 1| 201901| -|
| 1| 201902| X|
| 2| 201801| X|
| 2| 201802| X|
| 2| 201803| -|
| 2| 201804| -|
| 2| 201805| X|
+---+-------+----+
Basically, the monthid column should follow calander months.