Reputation: 15
So I have a DateTime column. What I need is to get the average number of intervals (by seconds) per person. So my dataframe currently looks like this:
No | AccountID | Agent | DateTime |
---|---|---|---|
0 | ##### | Agent A | 10/19/2021 15:00 |
1 | ##### | Agent B | 10/19/2021 14:13 |
2 | ##### | Agent C | 10/19/2021 15:24 |
3 | ##### | Agent D | 10/19/2021 10:54 |
4 | ##### | Agent E | 10/19/2021 10:51 |
Now what I wanted to do is.
create another column to have the interval between the current date/time vs prev date/time. Which Would look something like this:
No | AccountID | Agent | DateTime | Intervals in Seconds |
---|---|---|---|---|
0 | ##### | Agent A | 10/19/2021 15:00 | ? |
1 | ##### | Agent B | 10/19/2021 14:13 | ? |
2 | ##### | Agent C | 10/19/2021 15:24 | ? |
3 | ##### | Agent D | 10/19/2021 10:54 | ? |
4 | ##### | Agent E | 10/19/2021 10:51 | ? |
Be able to see the average interval per Agent on a monthly basis.
I did my research but I got nowhere. I don't need the entire solution…just some advice on where I can start.
Upvotes: 0
Views: 515
Reputation: 27515
You can use groupby
and diff
to calculate the differences. You need to sort the rows first. And to get the result in seconds use Series.astype
:
df.sort_values(['Agent', 'DateTime'], inplace=True)
df['interval'] = df.groupby('Agent').DateTime.diff().astype('timedelta64[s]')
Upvotes: 1