[Python] Groupby aggregate multiple columns with same function

Discussão em 'Python' iniciado por Stack, Abril 22, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to do groupbyof this table. I have multiple value columns some of them I want to sum & some I want to count.

    df_p = pd.DataFrame({'H1':['A1','A1','C1','B1','A1','C1'],
    'H2':['X1','Y1','Z1','X1','Y1','Z1'],
    'V1':[1,2,3,4,5,6],
    'V2':[10,20,30,40,50,60],
    'V3':[100,200,300,400,500,600],
    'V4':[1000,2000,3000,4000,5000,6000],
    'V5':[11,12,13,14,15,16],
    'V6':[110,120,130,140,150,160],
    'V7':[1100,1200,1300,1400,1500,1600],
    'V8':[1100,1200,1300,1400,1500,1600],})


    I am trying to achieve that like this, which ofcourse is not a correct syntax.

    df_p.groupby(['H1','H2']).agg({['V1','V2','V3']:'sum',['V4','V5','V6','V7','V8']:'count'})


    I can do it like this which is too much editing & I have ~30 columns to do that way.

    df_p.groupby(['H1','H2']).agg({'V1':'sum','V2':'sum','V3':'sum','V4':'sum','V5':'count','V6':'count','V7':'count','V8':'count'})


    I just feel like through dictionary, list, tuple something I should be able to simplify the operation.

    Continue reading...

Compartilhe esta Página