I am trying to perform an action in Python which is very similar to VLOOKUP in Excel. but based on the first part of a string, problem is that firt part is not of a certain lenghts.
Ex: I have refrence data of Gouna and GreenLand, but lookupvalue for Gouna sometimes starts with G and other times starts with Gou and for lookupvalues for GreenLand starts with Gre
I have the following two pandas dataframes:
df1 = pd.DataFrame({'Abb': ['G', 'GRE', 'Gou', 'B'],
'FullName': ['Gouna', 'GreenLand', 'Gouna', 'Bahr']})
df2 = pd.DataFrame({'OrderNo': ['INV20561', 'INV20562', 'INV20563', 'INV20564'],
'AreaName': ['GRE65335', 'Gou6D654', 'Gddd654', 'B65465']})
print(df1)
Abb FullName
0 G Gouna
1 GRE GreenLand
2 Gou Gouna
3 B Bahrain
print(df2)
OrderNo AreaName
0 INV20561 GRE65335
1 INV20562 Gou6D654
2 INV20563 Gddd654
3 INV20564 B65465
and my needed out put should be:
OrderNo AreaName FullName
0 INV20561 GRE65335 GreenLand
1 INV20562 Gou6D654 Gouna
2 INV20563 Gddd654 Gouna
3 INV20564 B65465 Bahr
My approach would be to sort the Abb values in the df1 descendingly by values length:
df1.sort_values(by="Abb", key=lambda x: x.str.len(), ascending=False)
Abb FullName
1 GRE GreenLand
2 Gou Gouna
0 G Gouna
3 B Bahrain
Then perform some sort with vlookup with for loop instead of or applying a custom function. and here is where I am stuck.