I have two dataframes as below, df1 and df2. I am looking to match two columns between these df's and then add a new column to df1 which includes other column data from df2. In this case df2 is in effect reference data and I am looking to add data from here to df1.
df1 =
Column A1 | Column B1 |
---|---|
x | 2 |
y | 3 |
df2 =
Column A2 | Column B2 | Column C2 | Column D2 |
---|---|---|---|
w | 1 | abc | usd |
x | 2 | def | eur |
y | 3 | ghi | gbp |
z | 4 | jkl | nzd |
In this case the match would be between df1[A1,B1] and df2[A2, B2] - where these match, the value from df2[D2] is taken and added to df1:
target_output =
Column A1 | Column B1 | Column D2 |
---|---|---|
x | 2 | eur |
y | 3 | gbp |
NB: The dfs are not of the same the length.
Any help would be greatly appreciated. Thanks.
I have tried matching and working through df1iteratively however receive errors when applying conditions for multiple matching. I also tried using df2 as a dictionary and referencing however I am still struggling to implement the multiple conditions on the match. I also attempted to merge the data frames however this returned multiple errors both in implementing logic and type errors between dfs.