title: Pyspark Replicate Row based on column value
body: <p>I would like to replicate all rows in my DataFrame based on the value of a given column on each row, and than index each new row. Suppose I have:</p>
  
  <pre class="lang-python prettyprint-override"><code>Column A Column B
  T1       3
  T2       2
  </code></pre>
  
  <p>I want the result to be:</p>
  
  <pre class="lang-python prettyprint-override"><code>Column A Column B Index
  T1       3        1
  T1       3        2
  T1       3        3
  T2       2        1
  T2       2        2
  </code></pre>
  
  <p>I was able to to something similar with fixed values, but not by using the information found on the column. My current working code for fixed values is:</p>
  
  <pre class="lang-python prettyprint-override"><code>idx = [lit(i) for i in range(1, 10)]
  df = df.withColumn('Index', explode(array( idx ) ))
  </code></pre>
  
  <p>I tried to change:</p>
  
  <pre class="lang-python prettyprint-override"><code>lit(i) for i in range(1, 10) 
  </code></pre>
  
  <p>to </p>
  
  <pre class="lang-python prettyprint-override"><code>lit(i) for i in range(1, df['Column B'])
  </code></pre>
  
  <p>and add it into my array() function:</p>
  
  <pre class="lang-python prettyprint-override"><code>df = df.withColumn('Index', explode(array( lit(i) for i in range(1, df['Column B']) ) ))
  </code></pre>
  
  <p>but it does not work (TypeError: 'Column' object cannot be interpreted as an integer). </p>
  
  <p>How should I implement this? </p>