我发现了一些意外的结果。我想做的是创建一个查看ID号和日期的列,并将计算过去7天该ID号出现的次数(我也想将其动态化x倍)天,但只需尝试7天即可。
因此,给出此数据框:
import pandas as pd
df = pd.DataFrame(
[['A', '2020-02-02 20:31:00'],
['A', '2020-02-03 00:52:00'],
['A', '2020-02-07 23:45:00'],
['A', '2020-02-08 13:19:00'],
['A', '2020-02-18 13:16:00'],
['A', '2020-02-27 12:16:00'],
['A', '2020-02-28 12:16:00'],
['B', '2020-02-07 18:57:00'],
['B', '2020-02-07 21:50:00'],
['B', '2020-02-12 19:03:00'],
['C', '2020-02-01 13:50:00'],
['C', '2020-02-11 15:50:00'],
['C', '2020-02-21 10:50:00']],
columns = ['ID', 'Date'])
用于计算每个实例在最近7天内发生的代码:
df['Date'] = pd.to_datetime(df['Date'])
delta = 7
df['count_in_last_%s_days' %(delta)] = df.groupby(['ID', pd.Grouper(freq='%sD' %delta, key='Date')]).cumcount()
输出:
ID Date count_in_last_7_days
0 A 2020-02-02 20:31:00 0
1 A 2020-02-03 00:52:00 1
2 A 2020-02-07 23:45:00 2
3 A 2020-02-08 13:19:00 0 #<---- This should output 3
4 A 2020-02-18 13:16:00 0
5 A 2020-02-27 12:16:00 0
6 A 2020-02-28 12:16:00 1
7 B 2020-02-07 18:57:00 0
8 B 2020-02-07 21:50:00 1
9 B 2020-02-12 19:03:00 0 #<---- THIS SHOULD OUTPUT 2
10 C 2020-02-01 13:50:00 0
11 C 2020-02-11 15:50:00 0
12 C 2020-02-21 10:50:00 0
答案 0 :(得分:4)
您不想在Grouper
上使用Date
,而是使用rolling
窗口。分组程序会将数据帧分割为所需持续时间的单独连续块。您希望从每个日期起7天,这是rolling
的工作:
delta = 7
df['count_in_last_%s_days' %(delta)] = df.assign(count=1).groupby(
['ID']).apply(lambda x: x.rolling('%sD' %delta, on='Date').sum(
))['count'].astype(int) - 1
它给出了预期的结果:
ID Date count_in_last_7_days
0 A 2020-02-02 20:31:00 0
1 A 2020-02-03 00:52:00 1
2 A 2020-02-07 23:45:00 2
3 A 2020-02-08 13:19:00 3
4 A 2020-02-18 13:16:00 0
5 A 2020-02-27 12:16:00 0
6 A 2020-02-28 12:16:00 1
7 B 2020-02-07 18:57:00 0
8 B 2020-02-07 21:50:00 1
9 B 2020-02-12 19:03:00 2
10 C 2020-02-01 13:50:00 0
11 C 2020-02-11 15:50:00 0
12 C 2020-02-21 10:50:00 0
答案 1 :(得分:1)
看起来像在Date
上滚动并具有正确的窗口可以做到:
(df.set_index('Date')
.assign(count_last=1)
.groupby('ID')
.rolling(f'{delta}D')
.sum() - 1
)
输出:
count_last
ID Date
A 2020-02-02 20:31:00 0.0
2020-02-03 00:52:00 1.0
2020-02-07 23:45:00 2.0
2020-02-08 13:19:00 3.0
2020-02-18 13:16:00 0.0
2020-02-27 12:16:00 0.0
2020-02-28 12:16:00 1.0
B 2020-02-07 18:57:00 0.0
2020-02-07 21:50:00 1.0
2020-02-12 19:03:00 2.0
C 2020-02-01 13:50:00 0.0
2020-02-11 15:50:00 0.0
2020-02-21 10:50:00 0.0