我有数据
date id request
0 2016-06-22 03:00:48 upi88@yandex.ru GET HTTP/1.1
1 2016-06-22 03:02:52 upi88@yandex.ru GET HTTP/1.1
2 2016-06-22 03:03:48 upi88@yandex.ru GET HTTP/1.1
3 2016-06-22 03:05:15 upi88@yandex.ru GET HTTP/1.1
4 2016-06-22 03:05:55 upi88@yandex.ru GET HTTP/1.1
我希望计算date
之间的差异
我试试
dates = df1['date']
for i, date in enumerate(dates):
dates[i].to_datetime()
d = (dates[i] - dates[i-1]).total_seconds()
但它返回
KeyError: -1L
答案 0 :(得分:0)
是你想要的吗?
In [130]: df
Out[130]:
date id request
0 2016-06-22 03:00:48 upi88@yandex.ru GET HTTP/1.1
1 2016-06-22 03:02:52 upi88@yandex.ru GET HTTP/1.1
2 2016-06-22 03:03:48 upi88@yandex.ru GET HTTP/1.1
3 2016-06-22 03:05:15 upi88@yandex.ru GET HTTP/1.1
4 2016-06-22 03:05:55 upi88@yandex.ru GET HTTP/1.1
In [131]: df.date.diff().dt.total_seconds()
Out[131]:
0 NaN
1 124.0
2 56.0
3 87.0
4 40.0
Name: date, dtype: float64
答案 1 :(得分:0)
你的代码起始索引是1而i-1是-1
dates = df1['date']
for i in range(1,len(dates)):
dates[i].to_datetime()
d = (dates[i] - dates[i-1]).total_seconds()