Guest User

Untitled

a guest
Dec 12th, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. df = pd.DataFrame({'id': [1,1,1,1,1,1,2,2,2,2,2,2],
  2. 'val': [1,10,1,10,20,30,1,1,1,12,17,28]})
  3.  
  4. id val
  5. 1 1
  6. 1 10
  7. 1 1
  8. 1 10
  9. 1 20
  10. 1 30
  11. 2 1
  12. 2 1
  13. 2 1
  14. 2 12
  15. 2 17
  16. 2 28
  17.  
  18. id val
  19. 1 10
  20. 1 20
  21. 1 30
  22. 2 12
  23. 2 17
  24. 2 28
  25.  
  26. df = pd.DataFrame({'id': [1,1,1,1,1,1],
  27. 'val': [1,10,1,10,20,30]})
  28.  
  29. # create groups at breakpoints where condition is no longer met
  30. g = df.groupby((df['val'] > 5).cumsum())
  31.  
  32. # find last group
  33. label = max(list(g.groups.keys()))
  34.  
  35. result = df.loc[g.groups[label]._data].tail(-1)
Add Comment
Please, Sign In to add comment