简体   繁体   中英

Calculate Average Number of Days Between Multiple Dates

Let's say I have the following data frame. I want to calculate the average number of days between all the activities for a particular account.

在此处输入图片说明

Below is my desired result:

在此处输入图片说明

Now I know how to calculate the number of days between two dates with the following code. But I don't know how to calculate what I am looking for across multiple dates.

from datetime import date

d0 = date(2016, 8, 18)
d1 = date(2016, 9, 26)
delta = d0 - d1
print delta.days

I would do this as follows in pandas (assuming the Date column is a datetime64):

In [11]: df
Out[11]:
  Account Activity       Date
0       A        a 2015-10-21
1       A        b 2016-07-07
2       A        c 2016-07-07
3       A        d 2016-09-14
4       A        e 2016-10-12
5       B        a 2015-11-24
6       B        b 2015-12-30

In [12]: df.groupby("Account")["Date"].apply(lambda x: x.diff().mean())
Out[12]:
Account
A   89 days 06:00:00
B   36 days 00:00:00
Name: Date, dtype: timedelta64[ns]

If your dates are in a list:

>>> from datetime import date
>>> dates = [date(2015, 10, 21), date(2016, 7, 7), date(2016, 7, 7), date(2016, 9, 14), date(2016, 10, 12), date(2016, 10, 12), date(2016, 11, 22), date(2016, 12, 21)]
>>> differences = [(dates[i]-dates[i-1]).days for i in range(1, len(dates))] #[260, 0, 69, 28, 0, 41, 29]
>>> float(sum(differences))/len(differences)
61.0
>>> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM