SHARE
TWEET

Untitled

a guest Jun 18th, 2019 43 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. df1             df2            
  2.  
  3. id value        id value      
  4.  a     5         a   3        
  5.  c     9         b   7        
  6.  d     4         c   6        
  7.  f     2         d   8        
  8.                  e   2        
  9.                  f   1
  10.      
  11. df3
  12. id value
  13. a      5
  14. b      7
  15. c      9
  16. d      4
  17. e      2
  18. f      2
  19.      
  20. In [80]: df1.set_index('id').combine_first(df2.set_index('id')).reset_index()
  21. Out[80]:
  22.   id  value
  23. 0  a    5.0
  24. 1  b    7.0
  25. 2  c    9.0
  26. 3  d    4.0
  27. 4  e    2.0
  28. 5  f    2.0
  29.      
  30. df1 = pd.DataFrame({'id': ['a', 'c', 'd', 'f'], 'value': [5, 9, 4, 2]})
  31. df2 = pd.DataFrame({'id': ['a', 'b', 'c', 'd', 'e', 'f'], 'value': [3, 7, 6, 8, 2, 1]})
  32. result = pd.merge(df2, df1, on='id', how='left', suffixes=('_x', ''))
  33. result['value'] = result['value'].fillna(result['value_x'])
  34. result = result[['id', 'value']]
  35. print(result)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top