我想算的次数每id, date的datetime是10秒钟前一行不同。
数据
id timestamp datetime date
1 1496660340 2019-06-05 10:59:00 2019-06-05
1 1496660340 2019-06-05 10:59:10 2019-06-05
1 1496660355 2019-06-05 10:59:40 2019-06-05 <- 30 sec diff from above, so not counted
1 1496655555 2019-06-06 11:58:00 2019-06-06
1 1496666666 2019-06-06 11:58:10 2019-06-06
1 1496666677 2019-06-06 11:58:20 2019-06-06
2 1496655555 2019-06-05 11:58:00 2019-06-05
2 1496666666 2019-06-05 11:58:10 2019-06-05
2 1496666677 2019-06-05 11:58:20 2019-06-05
Data columns (total 4 columns):
id int64
timestamp int64
datetime datetime64[ns]
date object
想要的
id date num_count
1 2019-06-05 1
1 2019-06-06 2
2 2019-06-05 2
我试过的
# get all the time differences first
df['timediff'] = df.groupby(['id','date'])['datetime'].diff() / np.timedelta64(1, 's')
#Count the number of 10sec differences
x = pd.DataFrame(df[df['timediff']==10].groupby(['id','date'],as_index=False)['timediff'].count())
我不确定这是否是正确的方法。有人可以指出我正确的方向吗?
相关分类