[Python] Pandas - group by and filter

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

  1. Stack

    Stack Membro Participativo

    Here is my dataframe

    my_df = pd.DataFrame({'col_1': ['A', 'A', 'B', 'B', 'C', 'C'],
    'col_2': [1, 2, 1, 2, 1, 2]})


    I would like to group by col_1 and filter out anything strictly greater than one using col_2. The final result should look like:

    final_df = pd.DataFrame({'col_1': ['A', 'B', 'C'],
    'col_2': [1, 1, 1, ]})


    Here is what I tried:

    df_ts = my_df.groupby('col_1').filter(lambda x: (x['col_2'] <= 1).any())


    It returns the same dataframe

    I also tried:

    df_ts = my_df.groupby('col_1').filter(lambda x: x['col_2'] <= 1)


    It generates error.

    Continue reading...

Compartilhe esta Página