import pandas as pd
import numpy as np
df1 = pd.DataFrame([[0, 1, 2, 3], [4, 5, 6, 7]], columns=['t', 'user_id', 'x', 'y'])
>>> df1
t user_id x y
0 0 1 2 3
1 4 5 6 7
df2 = pd.DataFrame([[0, 1, 0, 0], [4, 5, 0, 0], [5, 6, 7, 8]], columns=['t', 'user_id', 'x', 'y'])
>>> df2
t user_id x y
0 0 1 0 0
1 4 5 0 0
2 5 6 7 8
def f(row):
if np.isnan(row['x_y']):
row['x'], row['y'] = row['x_x'], row['y_x']
else:
row['x'], row['y'] = row['x_y'], row['y_y']
return row
res = pd.merge(df2, df1, how='left', on=['t', 'user_id']).apply(f, axis=1)[['t', 'user_id', 'x', 'y']]
>>> res
t user_id x y
0 0.0 1.0 2.0 3.0
1 4.0 5.0 6.0 7.0
2 5.0 6.0 7.0 8.0
to improve performance, you should reduce the line-by-line operation of pandas.DataFrame.apply () . In this case, you can use the numpy.where () binary operator, as follows
In [1]: import pandas as pd
In [2]: import numpy as np
In [3]: df1 = pd.DataFrame({'t': [1,2,3], 'user_id': [10,20,30], 'v': [1.1,2.2,3.3]})
In [4]: df1
Out[4]:
t user_id v
0 1 10 1.1
1 2 20 2.2
2 3 30 3.3
In [5]: df2 = pd.DataFrame({'t': [4,1,2], 'user_id': [40,10,20], 'v': [400,100,200]})
In [6]: df2
Out[6]:
t user_id v
0 4 40 400
1 1 10 100
2 2 20 200
In [7]: df3 = pd.merge(df1, df2, how='right', on=['t', 'user_id'])
In [8]: df3
Out[8]:
t user_id v_x v_y
0 1 10 1.1 100
1 2 20 2.2 200
2 4 40 NaN 400
In [9]: df3['v'] = np.where(np.isnan(df3.v_x), df3.v_y, df3.v_x)
In [10]: df3
Out[10]:
t user_id v_x v_y v
0 1 10 1.1 100 1.1
1 2 20 2.2 200 2.2
2 4 40 NaN 400 400.0
In [11]: del df3['v_x']
In [12]: del df3['v_y']
In [13]: df3
Out[13]:
t user_id v
0 1 10 1.1
1 2 20 2.2
2 4 40 400.0