[Python] how to pass several variables in for a pandas groupby

Discussão em 'Python' iniciado por Stack, Junho 7, 2025.

  1. Stack

    Stack Membro Participativo

    This code works:

    cohort = r'priority'
    result2025 = df.groupby([cohort],as_index=False).agg({'resolvetime': ['count','mean']})


    and this code works

    cohort = r'impactedservice'
    result2025 = df.groupby([cohort],as_index=False).agg({'resolvetime': ['count','mean']})


    and this code works

    result2025 = df.groupby(['impactedservice','priority'],as_index=False).agg({'resolvetime': ['count','mean']})


    but what is not working for me is defining the cohort variable to be

    cohort = r'impactedservice,priority' # a double-cohort
    result2025 = df.groupby([cohort],as_index=False).agg({'resolvetime': ['count','mean']})


    That gives error:

    KeyError: 'impactedservice,priority'


    How to properly define the cohort variable in this case?

    Continue reading...

Compartilhe esta Página