I have this dataset:
Date Stock Peak Trough
2002-01-01 33.78 False True
2002-01-02 34.19 False False
2002-01-03 35.44 False False
2002-01-04 36.75 True False
My goal is to add a new column 'Feature' with all the x previous stock prices leading up to the current one in each row. Assuming x = 3, it would look like this:
Date Stock Peak Trough Feature
2002-01-01 33.78 False True [NaN, NaN, 33.78]
2002-01-02 34.19 False False [NaN, 33.78, 34.19]
2002-01-03 35.44 False False [33.78, 34.19, 35.44]
2002-01-04 36.75 True False [34.19, 35.44, 36.75]
So the new column 'Feature' should be an array, the last value in the array should always be the corresponding price of the stock in that row.
I tried using shift() but I don't know how to add all the x previous values as an array to the dataframe. What's the best way to achieve my goal? Thanks!