Guest User

Untitled

a guest
Apr 26th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.32 KB | None | 0 0
  1. ID color begin_date end_date location
  2. 1 red 2017-01-01 2017-01-07 initial
  3. 1 green 2017-01-05 2017-01-07 nursing
  4. 1 blue 2017-01-07 2017-01-15 rehab
  5. 1 red 2017-01-11 2017-01-22 Health
  6. 2 red 2017-02-22 2017-02-26 initial
  7. 2 green 2017-02-26 2017-02-28 nursing
  8. 2 blue 2017-02-26 2017-02-28 rehab
  9. 3 red 2017-03-11 2017-03-22 initial
  10. 4 red 2017-04-01 2017-04-07 initial
  11. 4 green 2017-04-05 2017-04-07 nursing
  12. 4 blue 2017-04-10 2017-04-15 Health
  13.  
  14. ID first_site
  15. 1 rehab
  16. 2 nursing
  17. 3 home
  18. 4 Health
  19.  
  20. SELECT
  21. OVER( PARTITION ID CASE WHEN end_date[0] = begin_date THEN location
  22. WHEN location = 'Health' THEN 'Health'
  23. ELSE 'Home' end) AS [first_site]
  24. FROM table
  25.  
  26. def conditions(x):
  27. #compare each group first
  28. val = x.loc[x['begin_date'] == x['end_date'].iloc[0], 'location']
  29. #if at least one match (not return empty `Series` get first value)
  30. if not val.empty:
  31. return val.iloc[0]
  32. #if value is empty, check if value 'Health' exists within the group
  33. elif (x['location'] == 'Health').any():
  34. return 'Health'
  35. else:
  36. return 'Home'
  37.  
  38. final = df.groupby('ID').apply(conditions).reset_index(name='first_site')
Add Comment
Please, Sign In to add comment