1

I am stuck in a requirement to pick the top 5 rows sorted in desc in the data frame based on column var_count and for each unique col_name in the dataFrame.

I would like to have the output to be sorted in desc based on var_count in the attached screenshot Could someone please help?

I have attached the screenshot

My code:

df_ans
#df_ans.groupby('col_name')['var_name'].nlargest(5)
#top_col_values = pd.DataFrame(df_ans.groupby('col_name')['var_count'].nlargest(5))
top_col_values = df_ans.groupby('col_name').head(5)
top_col_values

enter image description here

SMR
  • 401
  • 4
  • 15

0 Answers0