title: how to denormalize data on a dataframe on spark
body: <p>I have a peculiar requirement to denormalize data like below:</p>
  
  <p>Source dataframe: <code>Key, item_desc</code></p>
  
  <p>Target dataframe: <code>Key, item_desc1, item_desc2, item_desc3, item_desc4</code></p>
  
  <p>for every 4 records on source dataframe I should create one record on target dataframe.</p>
  
  <p>source data:</p>
  
  <pre><code>Key, item_desc
  1, desc1
  1, desc2
  1, desc3
  1, desc4
  1, desc5
  1, desc6
  </code></pre>
  
  <p>target data:</p>
  
  <pre><code>key, item_desc1, item_desc2, item_desc3, item_desc4
  1, desc1, desc2, desc3, desc4
  1, desc5, desc6
  </code></pre>
  
  <p>Can anyone guide me on how to write this code?
  I have done a sample code to do it on scala like below:</p>
  
  <pre><code>var l = (1 to 102).toList
  var n = ""
  var j = 1
  for (i &lt;- l){
  n = n + l(j) + ","
  if (j%4 == 0) {
  println(n)
  n = ""
  }
  if (j+1 == l.size) println(n)
  j = j+1
  }
  </code></pre>
  
  <p>But, I should apply this logic to dataframe/rdd/list.</p>
  
  <p>Please help me on this!</p>