daramasala
daramasala

Reputation: 3030

Selecting multiple values with partial multiindex

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).

Upvotes: 0

Views: 30

Answers (1)

BENY
BENY

Reputation: 323226

You can try use isin to check

df1 = df1[df1.reset_index(level=[3]).index.isin(df2.index)]

Upvotes: 1

Related Questions