I'm trying to use one column's values to shift another columns values by that amount. Pandas shift()
, per the documentation, takes an integer, but is there a way to instead use a Series?
Current Code:
import pandas as pd
df = pd.DataFrame({ 'a':[1,2,3,4,5,6,7,8,9,10],
'b':[0,0,0,0,4,4,4,0,0,0]})
df['a'] = df['a'].shift(df['b'])
...which is of course not working.
Desired output:
a b
0 1 0
1 2 0
2 3 0
3 4 0
4 1 4
5 2 4
6 3 4
7 8 0
8 9 0
9 10 0
If it makes it easier, the shift will always be the same, so theoretically the 'b'
series could be True / False
or some other binary trigger, and the .shift()
could still be an integer. Feels a little hacky going that route, but it would get the job done.