I have a Pandas dataframe with one column of numbers, similar to this:
id - val
0 - 100
1 - 200
2 - 100
3 - 400
4 - 300
5 - 100
etc
What I would like to do is to add a second column which is a list/numpy array of the values from the previous 3 rows:
id - val - val_list
0 - 100 - [] # Or [NaN, NaN, NaN]
1 - 200 - [100] # Or [NaN, NaN, 100]
2 - 100 - [100, 200] # Or [NaN, 100, 200]
3 - 400 - [100, 200, 100]
4 - 300 - [200, 100, 400]
5 - 100 - [100, 400, 300]
etc
Any idea how to solve this efficiently, preferably without looping?
[nan, nan, 100.0]
?