I have two dataframes df1 and df2 (df1 significantly larger in amount of rows). First dataframe consists of five columns: Column 1 - contains postcodes, Column 2 to Column 5 - contains certain code. Second dataframe consists of 2 columns: Column 1 - Raw code, Column 2 - New code. The idea is to take the code from Column 2 in the first dataframe, search for the corresponding New Code in the second dataframe based on the match in the Code column, and then replace the value in Column 2 of the first dataframe with the New Code from the second dataframe:
Postcode Column 2 Column 3 Column 4 Column 5
BS105JJ 1 3 0 1
BS105JL 0 0 0 1
BS105JN 1 2 0 1
BS105JP 0 0 0 1
BS105JR 1 1 0 1
BS105JS 0 0 0 1
BS105JT 1 5 0 1
Code Code New
0 1
1 3
2 5
3 7
4 9
5 3
Expected result:
Postcode Column 2 Column 3 Column 4 Column 5
BS105JJ 3 3 0 1
BS105JL 1 0 0 1
BS105JN 3 2 0 1
BS105JP 1 0 0 1
BS105JR 3 1 0 1
BS105JS 1 0 0 1
BS105JT 3 5 0 1
I have tried to browse for solutions, but so far only found methods to vlookup equal dataframes. In perfect world I would like to find solution with reusable function (each column will need to perform this vlookup with different dataframe) but even if just method is shown - It is fine, I will figure out how to put it into function myself.