Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.77 KB | None | 0 0
  1. df2['new'] = np.where(df2[df2.first_id.isin(df1.id.values)], 1, 0)
  2.  
  3. df1 = pd.DataFrame(np.random.randint(0,5,size=(100, 1)), columns=list('A')) # random 1 column df
  4. df2 = pd.DataFrame(np.random.randint(0,5,size=(100, 1)), columns=list('B')) # random 1 column df
  5. df2["new"] = df2.apply(lambda row: 1 if row[0] == df1["A"][row.name] else 0, axis = 1) # lambda function to check if they match. row.name gets the index
  6. df2
  7.  
  8. In [387]: df1
  9. Out[387]:
  10. id
  11. 0 1
  12. 1 2
  13. 2 3
  14. 3 4
  15. 4 5
  16.  
  17. In [388]: df2
  18. Out[388]:
  19. first_id
  20. 0 7
  21. 1 6
  22. 2 5
  23. 3 1
  24. 4 3
  25.  
  26. In [389]: df2['new'] = df2.first_id.isin(df1.id).astype(np.int8)
  27.  
  28. In [390]: df2
  29. Out[390]:
  30. first_id new
  31. 0 7 0
  32. 1 6 0
  33. 2 5 1
  34. 3 1 1
  35. 4 3 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement