I have a data frame where one of the columns is Date (i.e. 5/29/2007
) and then there is col A and col B and a few other columns. The date and the value of col A and col B uniquely identify the row. I want to add columns to the data frame which are the historical values of other columns (not A or B). For example I have some row. Call it row i
and it has some value for col D. And I want to add a column to the data frame which corresponds to the value of col D but for a previous day. That is the value of col D where the row, call it row j
, has the same values for col A and col B as row i
but the date is the date of row i
minus one day. How can I do this efficiently?
Also I will want to do this for multiple columns, not just col D.
Here is an example of what I want. Below is my current Data frame csv.
Date, col-A, col-B, col-C, col-D
5/29/2007, A, B, 0, 1
5/29/2007, AA, Bf, 7, 124
5/29/2007, AV, Bf, 1, 4
5/30/2007, A, B, 10, 1
5/30/2007, AA, Bf, 3, 1
5/30/2007, AV, Bf, 1, 8
This is the desired output data I want my data frame to have
Date, col-A, col-B, col-C, col-D, col-D-1
5/30/2007, A, B, 10, 1, 1
5/30/2007, AA, Bf, 3, 1, 124
5/30/2007, AV, Bf, 1, 8, 4