Get statistics for each group (such as count, mean, etc) using pandas GroupBy?

前端 未结 7 1396
无人及你
无人及你 2020-11-21 06:06

I have a data frame df and I use several columns from it to groupby:

df[\'col1\',\'col2\',\'col3\',\'col4\'].groupby([\'col1\',\'co         


        
7条回答
  •  忘掉有多难
    2020-11-21 06:44

    To get multiple stats, collapse the index, and retain column names:

    df = df.groupby(['col1','col2']).agg(['mean', 'count'])
    df.columns = [ ' '.join(str(i) for i in col) for col in df.columns]
    df.reset_index(inplace=True)
    df
    

    Produces:

提交回复
热议问题