title: How to select the first row of each group?
body: <p>I have a DataFrame generated as follow:</p>
  
  <pre><code>df.groupBy($"Hour", $"Category")
    .agg(sum($"value") as "TotalValue")
    .sort($"Hour".asc, $"TotalValue".desc))
  </code></pre>
  
  <p>The results look like:</p>
  
  <pre class="lang-none prettyprint-override"><code>+----+--------+----------+
  |Hour|Category|TotalValue|
  +----+--------+----------+
  |   0|   cat26|      30.9|
  |   0|   cat13|      22.1|
  |   0|   cat95|      19.6|
  |   0|  cat105|       1.3|
  |   1|   cat67|      28.5|
  |   1|    cat4|      26.8|
  |   1|   cat13|      12.6|
  |   1|   cat23|       5.3|
  |   2|   cat56|      39.6|
  |   2|   cat40|      29.7|
  |   2|  cat187|      27.9|
  |   2|   cat68|       9.8|
  |   3|    cat8|      35.6|
  | ...|    ....|      ....|
  +----+--------+----------+
  </code></pre>
  
  <p>As you can see, the DataFrame is ordered by <code>Hour</code> in an increasing order, then by <code>TotalValue</code> in a descending order.</p>
  
  <p>I would like to select the top row of each group, i.e.</p>
  
  <ul>
  <li>from the group of Hour==0 select (0,cat26,30.9)</li>
  <li>from the group of Hour==1 select (1,cat67,28.5)</li>
  <li>from the group of Hour==2 select (2,cat56,39.6)</li>
  <li>and so on</li>
  </ul>
  
  <p>So the desired output would be:</p>
  
  <pre class="lang-none prettyprint-override"><code>+----+--------+----------+
  |Hour|Category|TotalValue|
  +----+--------+----------+
  |   0|   cat26|      30.9|
  |   1|   cat67|      28.5|
  |   2|   cat56|      39.6|
  |   3|    cat8|      35.6|
  | ...|     ...|       ...|
  +----+--------+----------+
  </code></pre>
  
  <p>It might be handy to be able to select the top N rows of each group as well.</p>
  
  <p>Any help is highly appreciated.</p>