Calculating average time difference among items grouped by a specific column

Viewed 153

I have the following dataframe:

userid | time     
1        22.01.2001 13:00
1        22.01.2001 13:05   
1        22.01.2001 13:07  
2        22.01.2001 14:00
2        22.01.2001 14:04   
2        22.01.2001 13:05  
2        22.01.2001 13:06  
3        22.01.2001 13:20  
3        22.01.2001 13:22  
4        22.01.2001 13:37  

What I want to obtain is a new column per user that stores the average time difference among the consecutive activities:

userid | avg_time_diff
1        3.5    #(5 + 2) / 2
2        2      #(4 + 1 + 1) / 3
3        2
4        0

To achieve this, do I need to loop trough each user and calculate the average time difference one by one? Or, is there a quicker way to achieve the same result?

1 Answers
Related