1

I have a master dataframe df1 which has unique column combinations ('a' and 'b')

df1:
a   b
A   1 
B   4 
K   5

I want to use these combinations to compare df2, iterating through each column and returning True or False based on whether the combination in df2 matches that of df1.

 df2:
a   b   c
A   1   TRUE
B   4   TRUE
K   1   FALSE 

For example the 3rd row in df2 K, 1 does not match the K value in df1 (K,5). Just fyi, the number of rows will be larger for df2

Thanks a lot for your help.

1 Answer 1

3

Use DataFrame.merge with indicator parameter and then compare by both value for boolean column:

df3 = df2.merge(df1, on=['a','b'], how='left',indicator='c')
df3['c'] = df3['c'].eq('both')

print (df3)
   a  b      c
0  A  1   True
1  B  4   True
2  K  1  False
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.