I have a dataframe that looks something like that (the date is in the format: dd/mm/yyyy
):
Param1 Param2 date value
1 a b 30/10/2007 5
2 a b 31/10/2007 8
3 a b 01/11/2007 9
4 a b 01/12/2007 3
5 a b 02/12/2007 2
6 a b 01/03/2008 11
7 b c 05/10/2008 7
8 b c 06/10/2008 13
9 b c 07/10/2008 19
10 b c 08/11/2008 22
11 b c 09/11/2008 35
12 b c 08/12/2008 5
what I need to do, is to group by Param1
and Param2
, and to create N (in my case, 3) additional columns for the 3 last previous values, that are at least 30 days away from the current row.
So the output should look something like that:
Param1 Param2 date value prev_1 prev_2 prev_3
1 a b 30/10/2007 5 None None None
2 a b 31/10/2007 8 None None None
3 a b 01/11/2007 9 None None None
4 a b 01/12/2007 3 9 8 5
5 a b 02/12/2007 2 9 8 5
6 a b 01/03/2008 11 2 3 9
7 b c 05/10/2008 7 None None None
8 b c 06/10/2008 13 None None None
9 b c 07/10/2008 19 None None None
10 b c 08/11/2008 22 19 13 7
11 b c 09/11/2008 35 19 13 7
12 b c 08/12/2008 5 22 19 13
I've tried using set_index
, stack
and related functions, but I just couldn't figure it out (without an ugly for
).
Any help will be appreciated!
EDIT: while it is similar to this question: question
It is not exactly the same, because you can't do a simple shift
as you need to check the condition of at least 30 days gap.