I know this is a basic question. But, please hear me out.
I have below dataframes:
In [722]: m1
Out[722]:
Person_id Evidence_14 Feature_14
0 100 90.0 True
1 101 NaN NaN
2 102 91.0 True
3 103 NaN NaN
4 104 94.0 True
5 105 NaN NaN
6 106 NaN NaN
In [721]: m3
Out[721]:
Person_id Evidence_14 Feature_14
0 100 NaN NaN
1 101 99.0 False
2 102 NaN NaN
3 103 95.0 False
4 104 NaN NaN
5 105 NaN NaN
6 106 93.0 False
Expected Output:
In [734]: z
Out[734]:
Person_id Evidence_14 Feature_14
0 100 90.0 True
1 101 99.0 False
2 102 91.0 True
3 103 95.0 False
4 104 94.0 True
5 105 NaN NaN
6 106 93.0 False
I am able to solve this like below:
In [725]: z = m1.merge(m3, on='Person_id')
In [728]: z['Evidence_14'] = z.Evidence_14_x.combine_first(z.Evidence_14_y)
In [731]: z['Feature_14'] = z.Feature_14_x.combine_first(z.Feature_14_y)
In [733]: z.drop(['Evidence_14_x', 'Evidence_14_y', 'Feature_14_x', 'Feature_14_y'], 1, inplace=True)
In [734]: z
Out[734]:
Person_id Evidence_14 Feature_14
0 100 90.0 True
1 101 99.0 False
2 102 91.0 True
3 103 95.0 False
4 104 94.0 True
5 105 NaN NaN
6 106 93.0 False
But, is there a cleaner/better way to do this? Am I missing something very obvious?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…