title: group the dataset rows based on the value
body: <p>I need the group rows of my dataset based on some criteria. My input dataset is like df1 :</p>
  <pre><code>+------------+----------+----------------------------+------------------+-------+
  |     col_1  |     col_2|col_3                       |            tp    |  range|
  +------------+----------+----------------------------+------------------+-------+
  |MP          |W         |                           X|10                |]0,3]  |
  |MP          |W         |                           X|20                |]12,30]|
  |MP          |W         |                           X|18                |]12,30]|
  |MP          |W         |                           X|18                |]0,3]  |
  |MP          |W         |                           X|30                |]0,3]  |
  |MP          |W         |                           X|50                |]12,30]|
  |MP          |W         |                           X|18                |]12,30]|
  |MP          |W         |                           X|60                |]12,30]|
  |MP          |W         |                           X|50                |]12,30]|
  |MP          |W         |                           X|70                |]12,30]|
  |MP          |W         |                           X|18                |]12,30]|
  |MP          |W         |                           X|90                |]12,30]|
  |MP          |W         |                           X|18                |]36,48]|
  |MP          |W         |                           X|18                |]36,48]|
  |MP          |W         |                           X|18                |]12,30]|
  |MP          |W         |                           X|180               |]12,30]|
  |MP          |W         |                           X|18                |]36,48]|
  |MP          |W         |                           X|18                |]12,30]|
  |MP          |W         |                         S2E|19                |]24,36]|
  |MP          |W         |                         S2E|40                |]24,36]|
  +------------+----------+----------------------------+------------------+-------+
  </code></pre>
  <p>What I would like to do is :</p>
  <ol>
  <li>Group rows of df1 by range (last column) [df = df1.select(&quot;*&quot;).groupby(&quot;col_1&quot;, &quot;col_2&quot;, &quot;col_3&quot;, &quot;tp&quot;, &quot;range&quot;]</li>
  <li>for rows in the same range, create subgroups where the ratio between 2 yield (column name = tp) of the same subgroup is less than 2 [i.e tp(i-1)/tp(i) &lt; 2 or tp(i-2)/tp(i) &lt; 2]</li>
  </ol>
  <p>In output for the range ]12,30], I will have something like :</p>
  <pre><code>+------------+----------+----------------------------+------------------+-------+------------+
  |     col_1  |     col_2|col_3                       |            tp    |  range|  subgroup  |
  +------------+----------+----------------------------+------------------+-------+------------+
  |MP          |W         |                           X|20                |]12,30]|subgroup_1  |
  |MP          |W         |                           X|18                |]12,30]|subgroup_1  |
  |MP          |W         |                           X|50                |]12,30]|subgroup_2  |
  |MP          |W         |                           X|18                |]12,30]|subgroup_1  |
  |MP          |W         |                           X|60                |]12,30]|subgroup_2  |
  |MP          |W         |                           X|50                |]12,30]|subgroup_2  |
  |MP          |W         |                           X|70                |]12,30]|subgroup_2  |
  |MP          |W         |                           X|90                |]12,30]|subgroup_2  |
  |MP          |W         |                           X|180               |]12,30]|subgroup_3  |
  +------------+----------+----------------------------+------------------+-------+------------+
  </code></pre>
  <p>Someone has a solution? I am working in Spark Java.</p>