I have a dataframe as following, just a example.
date y w diff
2010-1-1 3 1 3
2010-1-2 4 1 4
2010-1-3 5 1 2
2010-1-4 6 2 5
2010-1-5 7 2 6
2010-1-6 8 2 5
2010-1-7 9 3 2
2010-1-8 10 4 4
2010-1-9 11 5 5
2010-1-10 12 6 6
2010-1-11 13 5 6
Now for example i is the index of dataframe, I want to add new column for the dataframe, there are three new column name is like, p1, p2, p3, but the value is value of previous two date. Of course, the previous two rows of values p1, p2 is Nan. From 3-5 rows, the value of p1, p2 all are 3, 4, and value of p3 is value of last diff of previous two rows, I mean from 3-5 rows the value of p3 all are 4. I use the five rows as a period. I mean the 8-10 rows, the value of p1, p2, p3 are 8, 9, 2. The new dataframe like as following:
date y w diff p1 p2 p3
2010-1-1 3 1 3 Nan Nan Nan
2010-1-2 4 1 4 Nan Nan Nan
2010-1-3 5 1 2 3 4 4
2010-1-4 6 2 5 3 4 4
2010-1-5 7 2 6 3 4 4
2010-1-6 8 2 5 Nan Nan Nan
2010-1-7 9 3 2 Nan Nan Nan
2010-1-8 10 4 4 8 9 2
2010-1-9 11 5 5 8 9 2
2010-1-10 12 6 6 8 9 2
2010-1-11 13 5 6 Nan Nan Nan
If there are something you don't understand my question, please comment it. thanks!