[Python] Groupby and add calculated columns based on multiple conditions from other columns

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

  1. Stack

    Stack Membro Participativo

    I have a dataset that I want to groupby, and then add some calculated columns based on conditions from other columns. I want the status to only include 'open' and 'closed', and I want the state to exclude 'FL'.

    [​IMG]

    So far I've got:

    df.where(((df['status']=="open") or (df['status']=="closed"))
    and (df['state']!="FL").groupby(['schoolID', 'SchoolName']).agg(count=('StudentID', 'count'))


    but this returns an error:

    ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

    Continue reading...

Compartilhe esta Página