1

I want to pick only rows from df1 where both values of columns A and B in df1 match values of columns A and B in df2 so for example if df 1 and df2 are as follow:

df1 
A B C
1 2 3
4 5 6
6 7 8

df2
A B D E
1 2 6 8
2 3 7 9
4 5 2 1

the result will be a subset of df1 rows, in this example, result will look like:

df1
A B C
1 2 3
4 5 6

1 Answer 1

2

Use:

df = pd.merge(df1, df2[["A", "B"]], on=["A", "B"], how="inner")
print(df)

This prints:

   A  B  C
0  1  2  3
1  4  5  6
Sign up to request clarification or add additional context in comments.

5 Comments

df1.merge(df2[['A','B']]) will suffice, since default is inner and on all column. Not that there's anything wrong w/your answer just in case anyone was curious about the default behavior.
@Chris Right :)
Thanks @ShubhamSharma and Chris. I also have another question, is there an equivalent of "this" keyword for Pandas? For example, lets say I want to do df= df[df[c]>3] is there a way to do it in the same command as merge or as a chain command
@Mina Yes you can try, pd.merge(df1[df1["C"] > 3], df2[["A", "B"]], on=["A", "B"])
both great suggestions! Thanks a ton, honestly!

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.