Skip to main content
Added brief explanation
Source Link
jimmy
  • 496
  • 5
  • 13
FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]

Explanation:-

Idea is to get the indexes from second data frame and use them on first data frame. For multi-indexes you can pass the tuple of indexes to get the row.

FirstDF.loc[('bar','two'),] 

will give you all the rows whose first index is 'bar and second index is 'two'.

FirstDF.loc[(SecondDF['A'],SecondDF['B']),] 

takes those indexes directly from SecondDF which you want but the catch is it will take all the combinations of 'A' and 'B'. So adding zip will take only the indexes which are part of same row in SecondDF

FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]
FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]

Explanation:-

Idea is to get the indexes from second data frame and use them on first data frame. For multi-indexes you can pass the tuple of indexes to get the row.

FirstDF.loc[('bar','two'),] 

will give you all the rows whose first index is 'bar and second index is 'two'.

FirstDF.loc[(SecondDF['A'],SecondDF['B']),] 

takes those indexes directly from SecondDF which you want but the catch is it will take all the combinations of 'A' and 'B'. So adding zip will take only the indexes which are part of same row in SecondDF

deleted 6 characters in body
Source Link
hellow
  • 13.7k
  • 7
  • 66
  • 84

FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]

FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]

FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]

FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]
Source Link
jimmy
  • 496
  • 5
  • 13

FirstDF.loc[zip(SecondDF['A'],SecondDF['B']),]