[英]Find difference between dates in df using python
我有資料
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
那是你要的嗎?
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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.