Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.15 KB | None | 0 0
  1. d = ({
  2. 'Date' : ['1/2/18','1/2/18','1/2/18','1/2/18','1/2/18','1/2/18'],
  3. 'Val_A' : [1,1,2,2,1,1],
  4. 'Val_B' : ['X','X','X','X','Y','Y'],
  5. })
  6.  
  7. df = pd.DataFrame(data=d)
  8.  
  9. df1 = pd.pivot_table(df, index=['Date'],values=['Val_A','Val_B'],aggfunc= 'count')
  10.  
  11. Val_A Val_B
  12. Date
  13. 1/2/18 6 6
  14.  
  15. Val_X
  16. Date
  17. 1/2/18 3
  18.  
  19. pd.pivot_table(df.drop_duplicates(), index=['Date'],values=['Val_B'],aggfunc= 'count')
  20.  
  21. Val_B
  22. Date
  23. 1/2/18 3
  24.  
  25. df1 = pd.pivot_table(df.drop_duplicates(), index=['Date'],values=['Val_A','Val_B'],aggfunc= 'count')
  26.  
  27. df.drop_duplicates()
  28.  
  29. Date Val_A Val_B
  30. 0 1/2/18 1 X
  31. 2 1/2/18 2 X
  32. 4 2/2/18 1 Y
  33. 6 2/2/18 2 Y
  34.  
  35. df.groupby(['Date', 'Val_A', 'Val_B']).size().reset_index()
  36.  
  37. Date Val_A Val_B 0
  38. 0 1/2/18 1 X 2
  39. 1 1/2/18 1 Y 2
  40. 2 1/2/18 2 X 2
  41.  
  42. g = df.groupby(['Date', 'Val_A', 'Val_B'])
  43.  
  44. len(g)
  45.  
  46. # Out
  47. 3
  48.  
  49. df.drop_duplicates().groupby('Date').Val_A.count().reset_index(name='Val_x')
  50.  
  51.  
  52. Out[1996]:
  53. Date Val_x
  54. 0 1/2/18 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement