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?
If columns names matching and need match by Person_id
values use:
m = m1.set_index('Person_id').combine_first(m2.set_index('Person_id')).reset_index()
If index values are same and also Person_id
are same in both DataFrames solution should be simplify by matching with original index values:
m = m1.combine_first(m2)
As Person_id uniquely define related rows in m1 and m3, you have to use set_index. Look at this:
import pandas as pd
df1 = pd.DataFrame({'id':[11, 22, 33,44],'A': [None, 0, 17, None], 'B': [None, 4, 19,None]})
df2 = pd.DataFrame({'id':[111, 222], 'A': [9999, 9999], 'B': [7777, 7777]})
# df1 = df1.set_index('id')
# df2 = df2.set_index('id')
df1.combine_first(df2)
Out[32]:
id A B
0 11 9999.0 7777.0
1 22 0.0 4.0
2 33 17.0 19.0
3 44 NaN NaN
if you dont use set_index the first value of A will be changed even if it's id is 11 in df1 and 111 in df2 (different id)
Also note that if you use set_index, a non existing id in m1 will be Added to the result.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.