[Python] Groupby and Aggregate 2 columns in pandas

Discussão em 'Python' iniciado por Stack, Fevereiro 16, 2025.

  1. Stack

    Stack Membro Participativo

    I would like to summarize by dataset based on two columns and get both the 'sum' and 'count' of these columns. I know how to get the 'sum' and 'count' of the columns separately, for example:

    summary = df.groupby(['country','item', "date", 'msgtype'], as_index=False)['size', 'turnover'].sum()


    But I want to be able to do it in one line if possible, without having to do merges etc.

    I tried to do this:

    summary = df.groupby(['country','item', "date", 'order'], as_index=False).agg({(['size', 'weight']) : ['sum' , 'count']})


    But I get the error:

    unhashable type: 'list'


    So I'm not too sure what to do.

    Continue reading...

Compartilhe esta Página