Compare 2 datasets on multiple column criteria and find missing rows

I’m willing to compare two datasets:

First:

Partner Type Power Price
Partner1 Buy 1 15.975
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Sell 1000 43.5

Second:

Partner Type Power Price
Partner1 Buy 1 15.975
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Buy 1 18.025
Partner1 Buy 2 18.025
Partner1 Sell 5 19.05
Partner1 Sell 5 19.06
Partner1 Sell 5 19.125
Partner1 Buy 2 19.2

My goal is to check which rows from second table are NOT present in first table, BASED on equal values from column ‘Type’, ‘Price’ and ‘Power’.

compcol = ['Type','Power','Price']
missing = second[~second[compcol].isin(first[compcol].to_dict(
        orient='list')).all(axis=1)]

Above code does return missing rows that are missing in first table:

Partner Type Power Price
Partner1 Buy 2 18.025
Partner1 Sell 5 19.05
Partner1 Sell 5 19.06
Partner1 Sell 5 19.125
Partner1 Buy 2 19.2

What I want to achieve is to include one additional row of “Partner1 – BUY – 1 – 18.025”, that is also missing in first table (second table includes 5 record of that trade with same data, while first table includes only 4!).

How can I achieve that?

Thank you for your answers.

Answer

You can make use of boolean masking,isin() method, all() method and Bitwise not operator(~):

col=['Type','Power','Price']    
result=df2[~df2[col].isin(df1[col]).all(1)]

Now if you print result you will get your desired output:

Partner     Type    Power   Price

5   Partner1    Buy     1   18.025
6   Partner1    Buy     2   18.025
7   Partner1    Sell    5   19.050
8   Partner1    Sell    5   19.060
9   Partner1    Sell    5   19.125
10  Partner1    Buy     2   19.200