假设我有一个像这样的数据框
import pandas as pd
data = {
'id1': [1]*12 + [1]*12 + [2]*12 + [2]*12,
'id2': ['a']*12 + ['b']*12 + ['c']*12 + ['d']*12,
'date': (['2023-11-20', '2023-11-21', '2023-11-22', '2023-11-23', '2023-11-24', '2023-11-25', '2023-11-26', '2023-11-27', '2023-11-28', '2023-11-29', '2023-11-30', '2023-12-01']*4)[:48],
'event1': [10, 23, 36, 49, 62, 75, 88, 101, 114, 127, 140, 153, 12, 25, 38, 51, 64, 77, 90, 103, 116, 129, 142, 155, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 567, 785, 1003, 1221, 1439, 1657, 1875, 2093, 2311, 2529, 2747, 2965],
'event2': ([0]*12 + [3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25] + [45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]*2)[:48]
}
df = pd.DataFrame(data)
print(df)
正如您所看到的,对于每个 和的id2
值都是累积的。也就是说,两个连续日期之间的值差是两个日期中较大日期的事件计数。event1
event2
我想要为每个id2
计算过去一天(此处为 2023-11-30)的事件数、过去三天的事件总数和过去 7 天的事件总数。
我尝试过以下类型的东西:
df.set_index('date', inplace=True)
target_date = pd.to_datetime('2023-11-30')
events_on_target_date = df.loc[target_date, 'event1'] - df.loc[target_date - pd.DateOffset(days=1), 'event1']
events_3_days_before = df.loc[target_date - pd.DateOffset(days=3):target_date, 'event1'].iloc[-1] - df.loc[target_date - pd.DateOffset(days=4), 'event1']
events_5_days_before = df.loc[target_date - pd.DateOffset(days=5):target_date, 'event1'].iloc[-1] - df.loc[target_date - pd.DateOffset(days=6), 'event1']
print(f"Number of events on {target_date}: {events_on_target_date}")
print(f"Total number of events in the 3 days before {target_date}: {events_3_days_before}")
print(f"Total number of events in the 5 days before {target_date}: {events_5_days_before}")
它适用于一个独立的 id1 和 id2,但我遇到了困难:
- 将其应用到每个
id2
- 应用此包括计算
event1
和event2
期望的结果应该是这样的
id1 id2 count_event1_yesterday count_event1_past3Days count_event1_past5Days count_event2_yesterday count_event2_past3Days count_event2_past5Days
1 a 13 39 52 0 0 0
1 b 13 39 52 4 6 8
2 c
2 d
代码
分组依据 + 差异 + 最后
出去
您可以创建自定义聚合函数并按两者进行分组
idX
:输出: