groupby组内排序如取前n名 def sortv(x): return x.sort_values(ascending=False)[:5] tips.groupby(by='sex')['tip'].apply(sortv)