title: How to get top 3 sales in data frame after using group by and sorting in python?
body: <p>recently I am doing with this data set</p>
  <pre><code>    import pandas as pd
  
  data = {'Product':['Box','Bottles','Pen','Markers','Bottles','Pen','Markers','Bottles','Box','Markers','Markers','Pen'], 
          'State':['Alaska','California','Texas','North Carolina','California','Texas','Alaska','Texas','North Carolina','Alaska','California','Texas'], 
          'Sales':[14,24,31,12,13,7,9,31,18,16,18,14]}
   
  df1=pd.DataFrame(data, columns=['Product','State','Sales']) 
  df1
  </code></pre>
  <p>I want to find the 3 groups that have the highest sales</p>
  <pre><code>grouped_df1 = df1.groupby('State')
  grouped_df1.apply(lambda x: x.sort_values(by = 'Sales', ascending=False))
  </code></pre>
  <p>So I have a dataframe like this</p>
  <p><a href="https://i.stack.imgur.com/ls0Mp.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ls0Mp.png" alt="enter image description here" /></a></p>
  <p>Now, I want to find the top 3 State that have the highest sales.
  I tried to use</p>
  <pre><code>grouped_df1.apply(lambda x: x.sort_values(by = 'Sales', ascending=False)).head(3)
  # It gives me the first three rows
  grouped_df1.apply(lambda x: x.sort_values(by = 'Sales', ascending=False)).max()
  #It only gives me the maximum value
  </code></pre>
  <p>The expected result should be:</p>
  <pre><code>Texas: 31
  California: 24
  North Carolina: 18
  </code></pre>
  <p>Thus, how can I fix it? Because sometimes, a State can have 3 top sales, for example Alaska may have 3 top sales. When I simply sort it, the top 3 will be Alaska, and it cannot find 2 other groups.</p>
  <p>Many thanks!</p>