Guest User

Untitled

a guest
May 28th, 2018
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.80 KB | None | 0 0
  1. df = pd.DataFrame({'A': ['drama|Action', 'Drama', 'Action'], 'A_split1': ['Drama', 'Drama', 'Action'],'A_split2': ['Action', 'None', 'None'],'Drama': [0, 0, 0], 'Action': [0, 0, 0], 'Western': [0, 0, 0]},
  2. index = ['a1', 'a2', 'a3'])
  3. df
  4.  
  5. df = pd.DataFrame({'A': ['Drama|Action', 'Drama', 'Action'], 'B':range(3)},
  6. index = ['a1', 'a2', 'a3'])
  7. print (df)
  8. A B
  9. a1 Drama|Action 0
  10. a2 Drama 1
  11. a3 Action 2
  12.  
  13. df = df.join(df.pop('A').str.get_dummies())
  14. print (df)
  15. B Action Drama
  16. a1 0 1 1
  17. a2 1 0 1
  18. a3 2 1 0
  19.  
  20. df = df.join(df['A'].str.get_dummies())
  21. print (df)
  22. A B Action Drama
  23. a1 Drama|Action 0 1 1
  24. a2 Drama 1 0 1
  25. a3 Action 2 1 0
Add Comment
Please, Sign In to add comment