I am trying to find a neat solution to my problem. I have three tables:
Code DF
Code1 Code2 Code3 Code4 Code5
Eur xxx xxx xxx xxx
xxx xxx xxx ESP xxx
ASI xxx xxx xxx xxx
xxx BRA xxx xxx xxx
xxx AUS xxx xxx xxx
xxx xxx NOR xxx xxx
xxx xxx xxx PRT xxx
xxx xxx xxx xxx SGP
Country1 DF
Country-Code Region
Eur Europe
ASI Asia
BRA America
AUS Asia
NOR Europe
Country2 DF
Country Code Region
ESP Europe
PRT Europe
SGP Asia
ASI Asia
So what I want to do is, Create a Fifth Column which is Region.First I want to check the values in Code5 and Code4 respectively and if one of the codes matches Country2 dataframe then put its coresponding Region value in the Region column. If no matching code is found in Code5, go to Code4, if nothing there Code3 and etc. Code5 abbreviation and Code4 needs to look in Country2 dataframe and Code3,Code2 and Code1 need to look in Country1 dataframe. Just to clarify "xxx" can either be some other 3 letter abbreviation or empty space. There could also be Code and Regions that are similar between Country1 DF and Country2 DF, but there are some values that Code4 and Code5 should not match with Country1 df, that is why there are two different dataframes for matching. The case here is EUR, in Code1,Code2,Code3 is region Europe, but in Code4,Code5 it is currency and I do not want it to map to Europe if it consists in one of those two columns. The end case scenario needs to be something like this:
`Code1 Code2 Code3 Code4 Code5 Region
Eur xxx xxx xxx xxx Europe
xxx xxx xxx ESP xxx Europe
ASI xxx xxx xxx xxx Asia
xxx BRA xxx xxx xxx America
xxx AUS xxx xxx xxx Asia
xxx xxx NOR xxx xxx Europe
xxx xxx xxx PRT xxx Europe
xxx xxx xxx xxx SGP Europe
`