I have the following pandas DataFrame:
df = pd.DataFrame({'id': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
'x': ['A', 'A', 'A', 'A', 'C', 'C','E', 'G', 'G', 'G'],
'y': ['B', 'B', 'B', 'B', 'D', 'D', 'F', 'H', 'H', 'H'],
'year': [1990, 1991, 1992, 1993, 1994, 1999, 1999, 2001, 2002, 2010]})
id x y year
0 1 A B 1990
1 2 A B 1991
2 3 A B 1992
3 4 A B 1993
4 5 C D 1994
5 6 C D 1999
6 7 E F 1999
7 8 G H 2001
8 9 G H 2002
9 10 G H 2010
for each groupby(['x', 'y']), I need only the count of the ids in which the year figures between the min year of the group and year + 4
Expected result:
x y count_id
0 A B 4
1 C D 1
2 E F 1
3 G H 2