0

I have 2 dataframes.

df1 has a multi index of A, B, C and D. df2 has a multi index of A, B and C.

I want to select from df1 all the rows whose index appear in df2.

For example, if df2 contains a row with index A=1, B=1, C=1, I want to get all the rows from df1 with those values regardless of the value of D.

I tried doing df1[df2.index] and df1.loc[df2.index] but it does not work. I get "{ValueError}operands could not be broadcast together with shapes (13838,3) (4,) (13838,3) " (where 13838 is the length of df2).

1 Answer 1

1

You can try use isin to check

df1 = df1[df1.reset_index(level=[3]).index.isin(df2.index)]
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.