Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- df1 df2
- id value id value
- a 5 a 3
- c 9 b 7
- d 4 c 6
- f 2 d 8
- e 2
- f 1
- df3
- id value
- a 5
- b 7
- c 9
- d 4
- e 2
- f 2
- In [80]: df1.set_index('id').combine_first(df2.set_index('id')).reset_index()
- Out[80]:
- id value
- 0 a 5.0
- 1 b 7.0
- 2 c 9.0
- 3 d 4.0
- 4 e 2.0
- 5 f 2.0
- df1 = pd.DataFrame({'id': ['a', 'c', 'd', 'f'], 'value': [5, 9, 4, 2]})
- df2 = pd.DataFrame({'id': ['a', 'b', 'c', 'd', 'e', 'f'], 'value': [3, 7, 6, 8, 2, 1]})
- result = pd.merge(df2, df1, on='id', how='left', suffixes=('_x', ''))
- result['value'] = result['value'].fillna(result['value_x'])
- result = result[['id', 'value']]
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement