I have a pandas dataframe that looks like this:
Date Value shift_col
2021-02-11 50.12 0
2021-02-12 72.30 0
2021-02-15 81.87 1
2021-02-16 90.12 2
2021-02-17 91.31 3
2021-02-18 81.23 4
2021-02-19 73.45 6
2021-02-22 87.17 2
I want to shift the "Value" column by the value in the "shift_col" column. The shift column can be any integer, including zero and can be sequential or not.
I've tried lots of different approaches. Just using "df.Value.shift(df.shift_col) doesn't work (it gives me an error of ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I've tried a few options from these posts:
- Variable shift in Pandas. - doesn't work for date based indexes
- Pandas: Shift one column by other column value - seems to work but I really want to keep this pure pandas if possible with no other libraries.
This should be straightforward but it has me running around in circles chasing my own tail.
Any help would be appreciated.