[Python] Filter Pandas dataframe based on combination of two columns

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

  1. Stack

    Stack Membro Participativo

    Suppose I have a dataframe as,

    a b
    0 1 2
    1 2 3
    2 4 2
    3 4 3


    I want to filter the dataframe such that I get the result as,

    a b
    0 1 2
    3 4 3


    i.e, I want the combination (1,2) and (4,3) by filtering the two columns together.

    If I try this,

    df1 = df[df['a'].isin([1,4]) & df['b'].isin([2,3])]


    I get the entire dataframe back because combinations of (1,3) and (4,2) also gets included in the above method. But I need only the given combinations. I have a huge list of tuples of two columns based on which I want to filter the dataframe considering the corresponding tuple combination.

    Also, I dont want to merge the two columns together as a single string and then filter.

    Continue reading...

Compartilhe esta Página