Faced a problem. There is a Dataframe in which I need to calculate how much time has passed between operations for each user and indicate this difference in a separate column in this table. It turns out to calculate the time separately using DateTime, but how to do it in the table?
table = pd.DataFrame({
'user': ['Steve', 'Steve', 'Steve', 'Jack', 'Jack', 'Jack'],
'country':['UK', 'UK', 'UK', 'CH', 'CH', 'CH'],
'date': ['2018-01-15 00:05:07', '2018-01-15 00:06:14', '2018-01-15 00:08:36',
'2018-01-15 00:14:51', '2018-01-15 00:15:18', '2018-01-15 00:17:24']
})
table.set_index('country', inplace=True)
for i in table.groupby(['country', 'user']):
print(i)
In a separate column, you should get:
For Jack
- 00:14:51
- 00:00:27
- 00:02:06
For Steve
- 00:05:07
- 00:01:07
- 00:02:22