I have these two dataframes :
df = pd.DataFrame({'Points' : ['A','B','C','D','E'],'ColY' : [1,2,3,4,5]})
df
Points ColY
0 A 1
1 B 2
2 C 3
3 D 4
4 E 5
df2 = pd.DataFrame({'Points' : ['A','D'],'ColX' : [2,9]})
df2
Points ColX
0 A 2
1 D 9
And these two functions :
# equivalent of the Excel vlookup function applied to a dataframe
def vlookup(df,ref,col_ref,col_goal):
return pd.DataFrame(df[df.apply(lambda x: ref == x[col_ref],axis=1)][col_goal]).iloc[0,0]
# if x is in column Points of df2, return what is in column ColX in the same row
def update_if_belong_to_df2(x):
if x in df2['Points']:
return vlookup(df2,x,'Points','ColX')
return x
I would like to apply the function update_if_belong_to_df2 to the column ColY of df. I tried the following but it doesn't work :
df['ColY'] = df['ColY'].apply(lambda x : update_if_belong_to_df2(x))
I would like to get :
df
Points ColY
0 A 2
1 B 2
2 C 3
3 D 9
4 E 5
Could you please help me to understand why ? Thanks