title: Partition a Spark DataFrame based on values in an existing column into a chosen number of partitions
body: <p>I would like to partition a Spark DataFrame into an even number of partitions based on an index column before writing to a file. I would like to control how many partitions to create based on the size of the DataFrame and then use then when writing to Parquet file using <code>partitionBy</code>.</p>
  
  <p>Having an example DataFrame:</p>
  
  <pre><code> i     b
   0    11
   1     9
   2    13
   3     2
   4    15
   5     3
   6    14
   7    16
   8    11
   9     9
   10   17
   11   10
  </code></pre>
  
  <p>Assuming that I would like to create 4 partitions based on values in column <code>i</code> then the partitions would correspond to the values assigned to column <code>g</code>:</p>
  
  <pre><code>g    i     b
  0    0    11
  0    1     9
  0    2    13
  1    3     2
  1    4    15
  1    5     3
  2    6    14
  2    7    16
  2    8    11
  3    9     9
  3   10    17
  3   11    10
  </code></pre>
  
  <p>What is the preferred way of doing this in Spark?    </p>