title: PySpark, top for DataFrame
body: <p>What I want to do is given a DataFrame, take top n elements according to some specified column. The top(self, num) in RDD API is exactly what I want. I wonder if there is equivalent API in DataFrame world ?</p>
  
  <p>My first attempt is the following </p>
  
  <pre><code>def retrieve_top_n(df, n):
      # assume we want to get most popular n 'key' in DataFrame
      return df.groupBy('key').count().orderBy('count', ascending=False).limit(n).select('key')
  </code></pre>
  
  <p>However, I've realized that this results in non-deterministic behavior (I don't know the exact reason but I guess limit(n) doesn't guarantee which n to take)</p>