[Python] Pandas analogue to SQL MINUS / EXCEPT operator, using multiple columns

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

  1. Stack

    Stack Membro Participativo

    I'm looking for the fastest and idiomatic analog to SQL MINUS (AKA EXCEPT) operator.

    Here is what I mean - given two Pandas DataFrames as follows:

    In [77]: d1
    Out[77]:
    a b c
    0 0 0 1
    1 0 1 2
    2 1 0 3
    3 1 1 4
    4 0 0 5
    5 1 1 6
    6 2 2 7

    In [78]: d2
    Out[78]:
    a b c
    0 1 1 10
    1 0 0 11
    2 1 1 12


    How to find a result of d1 MINUS d2 taking into account only columns "a" and "b" in order to get the following result:

    In [62]: res
    Out[62]:
    a b c
    1 0 1 2
    2 1 0 3
    6 2 2 7


    MVCE:

    d1 = pd.DataFrame({
    'a': [0, 0, 1, 1, 0, 1, 2],
    'b': [0, 1, 0, 1, 0, 1, 2],
    'c': [1, 2, 3, 4, 5, 6, 7]
    })

    d2 = pd.DataFrame({
    'a': [1, 0, 1],
    'b': [1, 0, 1],
    'c': [10, 11, 12]
    })


    What have I tried:

    In [65]: tmp1 = d1.reset_index().set_index(["a", "b"])

    In [66]: idx = tmp1.index.difference(d2.set_index(["a","b"]).index)

    In [67]: res = d1.loc[tmp1.loc[idx, "index"]]

    In [68]: res
    Out[68]:
    a b c
    1 0 1 2
    2 1 0 3
    6 2 2 7


    it gives me correct results, but I have a feeling that there must be a more idiomatic and nicer / cleaner way to achieve that.

    PS DataFrame.isin() method won't help in this case as it'll produce a wrong result set

    Continue reading...

Compartilhe esta Página