0

I have two dataframes with MultiIndexes, and I need to right join them and then get the items on the right side which didn't have a key match.

The preferred answer for how to do right join where key is null in python pandas suggests to do something like this:

In [1]: dfa = pd.DataFrame({'A': range(5)}, index=range(5))

In [2]: dfb = pd.DataFrame({'A': range(10, 15)}, index=range(3,8))

In [3]: dfa
Out[3]: 
   A
0  0
1  1
2  2
3  3
4  4

In [4]: dfb
Out[4]: 
    A
3  10
4  11
5  12
6  13
7  14

In [5]: dfb.loc[dfb.index - dfa.index]
Out[5]: 
    A
5  12
6  13
7  14

but when I try that with my code, I get an error:

TypeError: cannot perform __sub__ with this index type: <class 'pandas.core.indexes.multi.MultiIndex'>

What options do I have given that MultiIndexes can't be subtracted from one another?

1

1 Answer 1

1

You do not need a join to get the items that did not match. A simpler way would be using the below:

df = dfb[dfb.index.isin(dfa.index) == False]

Alternate way, that uses join, would be:

a = dfa.join(dfb, lsuffix='a', how='right')
a[a.Aa.isnull()][['A']]

I do not know your exact requirement, but to get the result, 1st is probably a neater way of achieving the goal. Regards

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.