title: Pyspark - group by and select N highest values
body: <p>I have data like so:</p>
  <pre><code>+----------+----------+--------+
  | Location | Product  | Amount |
  +----------+----------+--------+
  | London   | Fish     |    307 |
  | London   | Chips    |    291 |
  | London   | Beer     |    147 |
  | Paris    | Baguettes|    217 |
  | Paris    | Cheese   |    103 |
  | Paris    | Champagne|     74 |
  +----------+----------+--------+
  </code></pre>
  <p>Naturally there are many locations and many products per location. I want to end up at a dataframe like this:</p>
  <pre><code>+----------+---------------------+-------------------------+-------+-------------------------+
  | Location | Most Common Product | 2nd Most Common Product |.....  | Nth Most Common Product |
  +----------+---------------------+-------------------------+-------+-------------------------+
  | London   | Fish                | Chips                   | ....  |     something           |
  | Paris    | Baguettes           | Cheese                  | ....  |     something else      |
  +----------+---------------------+-------------------------+-------+-------------------------+
  </code></pre>
  <p>I have figured out getting the most common, using <a href="https://stackoverflow.com/questions/48829993/groupby-column-and-filter-rows-with-maximum-value-in-pyspark">this</a>.</p>
  <p>In extending it to the N most common, I could create another dataframe with those rows removed, run the process again to get the 2nd most common, and join them together by Location. With appropriate column naming this could be put into a loop to run N times adding a column each iteration.</p>
  <p>However, this is going to be very slow since it would partition and join every iteration. How could I get, for example, the 50 most common per location, in a better way?</p>