I have a hc dataframe containing 2 columns called "Previous Cost Center" & "Current Cost Center" and would like to add two new columns called "Previous/Current Grouping" which should be filled with the grouping of the cost centers based on a mapping table with 2 columns named "CostCenter" & Grouping.
In excel I would just apply vlookup to fill the fields based on the cost center in each respective column and map it by the mapping table.
I've been playing around with the merge function but I couldn't get it to work.
How can I merge dataframe based on specified columns into a specified column?
HC List:
ID Cost Center Previous Cost Center Curent
0 1 A NaN
1 2 B C
2 3 NaN NaN
3 4 D D
4 5 F F
5 6 NaN A
6 7 A B
7 8 NaN B
8 9 A C
9 10 B D
10 11 D F
11 12 C NaN
Mapping:
Cost Center Grouping
0 A XERX
1 B ID
2 C BLOC
3 D KLRO
4 E PPP
5 F SDF
6 G D
Output:
ID Cost Center Previous Grouping Previous Cost Center Current Grouping Current
0 1 A XERX NaN NaN
1 2 B ID C BLOC
2 3 NaN NaN NaN NaN
3 4 D KLRO D KLRO
4 5 F SDF F SDF
5 6 NaN NaN A XERX
6 7 A XERX B ID
7 8 NaN NaN B ID
8 9 A XERX C BLOC
9 10 B ID D KLRO
10 11 D KLRO F SDF
11 12 C BLOC NaN NaN