Count and Sort with Pandas
I think you need add reset_index, then parameter ascending=False to sort_values because sort return: FutureWarning: sort(columns=….) is deprecated, use sort_values(by=…..) .sort_values([‘count’], ascending=False) df = df[[‘STNAME’,’CTYNAME’]].groupby([‘STNAME’])[‘CTYNAME’] \ .count() \ .reset_index(name=”count”) \ .sort_values([‘count’], ascending=False) \ .head(5) Sample: df = pd.DataFrame({‘STNAME’:list(‘abscscbcdbcsscae’), ‘CTYNAME’:[4,5,6,5,6,2,3,4,5,6,4,5,4,3,6,5]}) print (df) CTYNAME STNAME 0 4 a 1 5 b 2 6 s 3 5 c … Read more