I have two pandas data frames. They rows need to be matched by two values (X,Y) coordinates which are not exact matches but within a tolerance.. I need to add the 'VAL' term from df2 to the 'VAL' term from df1 where (X,Y) match.
I've attempted using merge_asof without luck. I think if I could get the merge_asof to work the way I'm expecting I can do the addition and re-assignment to df2 but so far I'm unable to get the matching to work out.
Here is an example of the code I have attempted.
d1 = [ ['wp1',0.0,0.0,10],['wp2',10.0,10.0,5],['wp3',7.0,7.0,5]]
d2 = [ ['wp4',0.1,0.1,5],['wp5',10.2,10.2,8]]
headers = ['Name','X','Y','VAL']
df1 = pd.DataFrame(d1,columns = headers)
df2 = pd.DataFrame(d2,columns = headers)
#attempt at solution just to find matching values
df3 = pd.merge_asof(df1.sort_values('VAL'), df2.sort_values('VAL'),on=['VAL'],by=['X','Y'],direction='nearest',tolerance=1)
#desired solution is to sum 'VAL' from df2 into df1
d1_final = [ ['wp1',0.0,0.0,10+5],['wp2',10.0,10.0,5+8]]
df1_final = pd.DataFrame(d1_final,columns = headers)