我有数据
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
之间的差值I try
dates = df1['date']
for i, date in enumerate(dates):
dates[i].to_datetime()
d = (dates[i] - dates[i-1]).total_seconds()
但是它返回
KeyError: -1L
这是你想要的吗?
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且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()