Guest User

Untitled

a guest
Oct 23rd, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.91 KB | None | 0 0
  1. import pandas as pd
  2. import numpy as np
  3. ycap = [2015, 2016, 2017]
  4.  
  5. df = pd.DataFrame({'a': np.repeat(ycap, 5),
  6. 'b': np.random.randn(15)})
  7.  
  8. a b
  9. 0 2015 0.436967
  10. 1 2015 -0.539453
  11. 2 2015 -0.450282
  12. 3 2015 0.907723
  13. 4 2015 -2.279188
  14. 5 2016 1.468736
  15. 6 2016 -0.169522
  16. 7 2016 0.003501
  17. 8 2016 0.182321
  18. 9 2016 0.647310
  19. 10 2017 0.679443
  20. 11 2017 -0.154405
  21. 12 2017 -0.197271
  22. 13 2017 -0.153552
  23. 14 2017 0.518803
  24.  
  25. a b c
  26. 0 2015 -0.826946 2014
  27. 1 2015 0.275072 2013
  28. 2 2015 0.735353 2012
  29. 3 2015 1.391345 2011
  30. 4 2015 0.389524 2010
  31. 5 2016 -0.944750 2015
  32. 6 2016 -1.192546 2014
  33. 7 2016 -0.247521 2013
  34. 8 2016 0.521094 2012
  35. 9 2016 0.273950 2011
  36. 10 2017 -1.199278 2016
  37. 11 2017 0.839705 2015
  38. 12 2017 0.075951 2014
  39. 13 2017 0.663696 2013
  40. 14 2017 0.398995 2012
  41.  
  42. gp = df.groupby('a')
  43. df['c'] = gp['a'].apply(lambda x: x-1)
Add Comment
Please, Sign In to add comment