Techie Fort
Techie Fort

Reputation: 462

Pandas: Get Rows in 1st dataframe with same values (in two columns) in another dataframe

I've two data frames A and B with columns col1, col2, col3.

I want to get all rows in A having same values in column col2, col3 of A AND B.

P.S. The index of rows in A and B having same values in col2 and col3 are not necessarily same.

Upvotes: 0

Views: 1234

Answers (1)

jezrael
jezrael

Reputation: 862681

IIUC you can try merge:

print A
  col1 col2 col3
0   aa    1    2
1   bb    2    5

print B
  col1 col2 col3
0   aa    1    2
1   aa    1    3
2   bb    2    5

print pd.merge(A,B, on=['col1','col2','col3'], how='left')
  col1 col2 col3
0   aa    1    2
1   bb    2    5

Upvotes: 2

Related Questions