[python]返回每个分组的top_n

来源:互联网 发布:淘宝联盟 高佣金 编辑:程序博客网 时间:2024/06/15 10:47
def top_n(df, n=3, column='APM'):    """        返回每个分组按 column 的 top n 数据    """    return df.sort_values(by=column, ascending=False)[:n]df_data.groupby('LeagueIndex').apply(top_n)

禁止分组 group_keys=False

df_data.groupby('LeagueIndex', group_keys=False).apply(top_n)
# apply函数接收的参数会传入自定义的函数中df_data.groupby('LeagueIndex').apply(top_n, n=2, column='Age')
原创粉丝点击