Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. import pandas as pd
  2. df = pd.DataFrame({'A': ['a', 'b', 'a'],
  3. 'B': ['b', 'a', 'c']})
  4. print(pd.get_dummies(df))
  5.  
  6. # A_a A_b B_a B_b B_c
  7. # 0 1 0 0 1 0
  8. # 1 0 1 1 0 0
  9. # 2 1 0 0 0 1
  10.  
  11. df = pd.DataFrame({('i','A'): ['a', 'b', 'a'],
  12. ('ii','B'): ['b', 'a', 'c']})
  13. ret = pd.get_dummies(df)
  14. print(ret)
  15. print(type(ret.columns[0]))
  16.  
  17. # ('i','A')_a ('i','A')_b ('ii','B')_a ('ii','B')_b ('ii','B')_c
  18. # 0 1 0 0 1 0
  19. # 1 0 1 1 0 0
  20. # 2 1 0 0 0 1
  21. #
  22. # str
  23.  
  24. # ('i','A','a') ('i','A','b') ('ii','B','a') ('ii','B','b') ('ii','B','c')
  25. # 0 1 0 0 1 0
  26. # 1 0 1 1 0 0
  27. # 2 1 0 0 0 1
  28.  
  29. # i ii
  30. # A B
  31. # a b a b c
  32. # 0 1 0 0 1 0
  33. # 1 0 1 1 0 0
  34. # 2 1 0 0 0 1
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement