Код: Выделить всё
Date Student_ID Exam_Score
2020-12-24 1 79
2020-12-24 3 100
2020-12-24 4 88
2021-01-19 1 100
2021-01-19 2 100
2021-01-19 3 99
2021-01-19 4 72
2022-09-30 3 100
2022-09-30 2 100
2022-09-30 1 100
2022-09-30 5 46
2023-04-23 3 100
2023-04-23 2 97
2023-04-23 1 100
2024-07-19 2 89
2024-07-19 1 100
2024-07-19 4 93
2024-07-19 3 100
2024-09-19 1 100
2024-09-19 2 80
2024-09-19 3 100
2024-09-19 4 80
2024-10-20 1 80
2024-10-20 3 99
2024-10-20 2 80
Код: Выделить всё
Date Student_ID Exam_Score Recent_Full_Marks
2020-12-24 1 79 0
2020-12-24 3 100 0
2020-12-24 4 88 0
2021-01-19 1 100 0
2021-01-19 2 100 0
2021-01-19 3 99 1
2021-01-19 4 72 0
2022-09-30 3 100 0
2022-09-30 2 100 0
2022-09-30 1 100 0
2022-09-30 5 46 0
2023-11-23 3 100 0
2023-11-23 2 97 0
2023-11-23 1 100 0
2024-07-19 2 89 0
2024-07-19 1 100 1
2024-07-19 4 93 0
2024-07-19 3 100 1
2024-09-19 1 100 0
2024-09-19 2 80 0
2024-09-19 3 100 0
2024-09-19 4 80 0
2024-10-20 1 100 1
2024-10-20 3 99 1
2024-10-20 2 80 0
2024-11-22 1 70 2
2024-11-22 3 100 1
2024-11-22 2 78 0
Код: Выделить всё
Date = pd.to_datetime(df['Date'], dayfirst=True)
full = (df.assign(Date=Date)
.sort_values(['Student_ID','Date'], ascending=[True,True])
['Exam_Score'].eq(100))
df['Recent_Full_Marks']=(full.groupby([df['Student_ID'], Date.dt.year], group_keys=False).apply(lambda g: g.shift(1, fill_value=0).cumsum()))
Подробнее здесь: https://stackoverflow.com/questions/790 ... -using-gro