Home > Software engineering >  Get list of indices where key-value doens't match both dataframes
Get list of indices where key-value doens't match both dataframes

Time:05-03

I have two dataframes.

left = pd.DataFrame(
    {
        "key": ["K0", "K1", "K2", "K3"],
        "A": ["A0", "A1", "A2", "A3"],
        "B": ["B0", "B1", "B2", "B3"],
    }
)


right = pd.DataFrame(
    {
        "key": ["K0", "K1", "K2", "K3", "K4", "K5"],
        "C": ["C0", "C1", "C2", "C3", "C4", "C5"],
        "D": ["D0", "D1", "D2", "D3", "D4", "D5"],
    }
)

The left df is part of the right df, as you can see:

left = 
    key A   B
0   K0  A0  B0
1   K1  A1  B1
2   K2  A2  B2
3   K3  A3  B3

right = 
    key C   D
0   K0  C0  D0
1   K1  C1  D1
2   K2  C2  D2
3   K3  C3  D3
4   K4  C4  D4
5   K5  C5  D5

Based on the key there is a difference in K4 and K5 between both dataframes.

My goal is to get a list of indices who are NOT in both dataframes, e.g. [4, 5] based on the key-value.

CodePudding user response:

You can use isin:

right[~right['key'].isin(left['key'])]

or for only the indices:

right.index[~right['key'].isin(left['key'])]
  • Related