0

I know this is a frequently asked question, but I can't manage to make this run.

I have managed to group by a dataframe and find the count based on some other data. Now based on pandas, I want to find to return the top 5 cuisines per country based on the count.

My data is called df3:

country cuisines count
Australia french 12
Australia italian 19
Italy. Italian 3
France Japanese 5
France Thailand 9

So far I tried the following, to no avail


df3=df3.groupby(['country','cuisines'])['count'].nlargest(5).reset_index()

df3=df3[['country','cuisines']].sort_values(['count'], ascending=False).head(5)

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
jeannetton
  • 27
  • 4
  • 1
    You want the top 5 per _country_ so your group should _only_ consider the country. Sort then groupby country `df3 = df3.sort_values('count', ascending=False).groupby('country').head(5)` as described by [this answer](/a/50415831/15497888) – Henry Ecker Nov 25 '22 at 18:14
  • 1
    You can also stash the column in the index `df3 = df3.set_index('cuisines').groupby('country')['count'].nlargest(5).reset_index()` as described [here](/a/42266472/15497888) – Henry Ecker Nov 25 '22 at 18:18
  • thank you for your help, that worked! I guessI got confused in the order of operations -> sorting first, grouping on one column instead of multiple. Thank you for your help ! – jeannetton Nov 25 '22 at 18:34

0 Answers0