I have a puzzle. This is easy in excel. But, in pandas, with the dataframe df:
| EventID | PictureID | Date
0 | 1 | A | 2010-01-01
1 | 2 | A | 2010-02-01
2 | 3 | A | 2010-02-15
3 | 4 | B | 2010-01-01
4 | 5 | C | 2010-02-01
5 | 6 | C | 2010-02-15
Is there a way to add a new column which counts the number of times the same PictureID has a recorded event in the previous 6 months? In other words, the number of rows in the dataframe with the same PictureID as a given row and with a Date within the six months prior to the date of the given row.
df['PastSix'] = ???
So the output looks something like:
| EventID | PictureID | Date | PastSix
0 | 1 | A | 2010-01-01 | 0
1 | 2 | A | 2010-02-01 | 1
2 | 3 | A | 2010-02-15 | 2
3 | 4 | B | 2010-01-01 | 0
4 | 5 | C | 2010-02-01 | 0
5 | 6 | C | 2010-02-15 | 1