I have two dataframes that i need to compare on two columns based on one condition.
df1 looks like this;
| Name | Value_1 | Value_2 |
|---|---|---|
| A | cat | pat |
| B | mat | mat |
| C | pat | sat |
df2 looks like this;
| Name | Value_1 | Value_2 |
|---|---|---|
| D | pat | cat |
| E | bat | mat |
| F | tot | pat |
I would like to match Value_1 and Value_2 from both the dataframes and return the Names from both the data frames when there is match in either value_1 or value_2.
result should look like this- It should have the names from both df1 and df2 if there is a match for value_1 or value_2.
| Name_x | Value | Name_y |
|---|---|---|
| A | pat | D |
| B | bat | E |
| C | tot | F |
I have tried pd.merge with 'on' condition and 'left_on' and 'right_on'. It works on matching one column. I cannot specify either/or condition. pd.merge only returns a result if there is a match in both value_1 and value_2. How do i get the Names when either value_1 or value_2 from df1 has a match with either value_1 or value_2 in df2?