0

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.

pltnrd
  • 1
  • 1
  • why would you have Cell 6 in the second row in D2? You likely want a left [`merge`](https://stackoverflow.com/questions/53645882/pandas-merging-101) – mozway Nov 15 '22 at 10:50
  • ** Have changed column values for more clarity. x = df1.merge(df, how='left', left_on=(['Column A1', 'Column B'])1 right_on=(['Column A2', 'Column B2']) ) When running this the added columns that are returned only contain NaN values. – pltnrd Nov 15 '22 at 11:17
  • Then it's a merge, see duplicate link – mozway Nov 15 '22 at 11:20
  • x = df1.merge(df, how='left', left_on=(['Column A1', 'Column B'])1 right_on=(['Column A2', 'Column B2']) ) When running this the added columns that are returned only contain NaN values - any idea if this would be incorrect? – pltnrd Nov 15 '22 at 11:20

0 Answers0