Python: Add array as new column containing x-previous values for each row

Viewed 33

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!

1 Answers

I think you need first add NaNs and then this solution, last remove first only NaNs list by indexing by [1:]:

n = 3
x = np.concatenate([[np.nan] * (n), df['Stock'].values])

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

df['new'] = rolling_window(x, n)[1:].tolist()
print (df)
         Date  Stock   Peak  Trough                    new
0  2002-01-01  33.78  False    True      [nan, nan, 33.78]
1  2002-01-02  34.19  False   False    [nan, 33.78, 34.19]
2  2002-01-03  35.44  False   False  [33.78, 34.19, 35.44]
3  2002-01-04  36.75   True   False  [34.19, 35.44, 36.75]
Related