【发布时间】:2021-11-11 03:18:02
【问题描述】:
遇到了问题。有一个数据框,我需要在其中计算每个用户的操作之间经过了多少时间,并在此表的单独列中指出这种差异。原来是用DateTime单独计算时间,但是在表中怎么做呢?
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)
在单独的列中,您应该得到:
给杰克
- 00:14:51
- 00:00:27
- 00:02:06
给史蒂夫
- 00:05:07
- 00:01:07
- 00:02:22
【问题讨论】:
标签: python pandas dataframe datetime pandas-groupby