Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pandas as pd
- import numpy as np
- ycap = [2015, 2016, 2017]
- df = pd.DataFrame({'a': np.repeat(ycap, 5),
- 'b': np.random.randn(15)})
- a b
- 0 2015 0.436967
- 1 2015 -0.539453
- 2 2015 -0.450282
- 3 2015 0.907723
- 4 2015 -2.279188
- 5 2016 1.468736
- 6 2016 -0.169522
- 7 2016 0.003501
- 8 2016 0.182321
- 9 2016 0.647310
- 10 2017 0.679443
- 11 2017 -0.154405
- 12 2017 -0.197271
- 13 2017 -0.153552
- 14 2017 0.518803
- a b c
- 0 2015 -0.826946 2014
- 1 2015 0.275072 2013
- 2 2015 0.735353 2012
- 3 2015 1.391345 2011
- 4 2015 0.389524 2010
- 5 2016 -0.944750 2015
- 6 2016 -1.192546 2014
- 7 2016 -0.247521 2013
- 8 2016 0.521094 2012
- 9 2016 0.273950 2011
- 10 2017 -1.199278 2016
- 11 2017 0.839705 2015
- 12 2017 0.075951 2014
- 13 2017 0.663696 2013
- 14 2017 0.398995 2012
- gp = df.groupby('a')
- df['c'] = gp['a'].apply(lambda x: x-1)
Add Comment
Please, Sign In to add comment