Advertisement
Guest User

Untitled

a guest
Jul 19th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. import pandas as pd
  2. from functools import reduce
  3.  
  4. ###### TEST BENJI
  5.  
  6. t = [
  7.     {"word": "a", "pos": 1},
  8.  
  9.     {"word": "b", "pos": 1},
  10.  
  11.     {"word": "c", "pos": 1},
  12.     {"word": "c", "pos": 1},
  13.     {"word": "c", "pos": 1},
  14.     {"word": "c", "pos": 4},
  15.     {"word": "c", "pos": 1},
  16.     {"word": "c", "pos": 1},
  17.  
  18.     {"word": "c", "pos": 2}
  19.  
  20. ]
  21.  
  22. df = pd.DataFrame(t)
  23.  
  24. k = df.groupby(by="word")['pos'].apply(list)
  25. result_word = []
  26. result_pos = []
  27.  
  28. for key, v in k.items():
  29.     result_word.append(key)
  30.     result_pos.append(reduce(lambda a, b : a | b, v))
  31.  
  32. result = pd.DataFrame({"word" : result_word, "pos" : result_pos})
  33.  
  34. print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement