我想获取end time
的{{1}}与下一个id
之间的时间间隔。
start time
import pandas as pd
df = pd.DataFrame({'id':[1,1,2,2,2],'startTime': ['2017-03-09 10:05:23', '2017-03-09 10:09:53', '2017-03-09 10:11:03' , '2017-03-09 10:41:04' , '2017-03-09 17:02:56' ],
'endTime':['2017-03-09 10:05:33' , '2017-03-09 10:09:58' , '2017-03-09 10:29:43' , '2017-03-09 12:41:24' , '2017-03-09 17:03:21' ]})
print(df)
Input:
id startTime endTime
0 1 2017-03-09 10:05:23 2017-03-09 10:05:33
1 1 2017-03-09 10:09:53 2017-03-09 10:09:58
2 2 2017-03-09 10:11:03 2017-03-09 10:29:43
3 2 2017-03-09 10:41:04 2017-03-09 12:41:24
4 2 2017-03-09 17:02:56 2017-03-09 17:03:21
Expected:
答案 0 :(得分:1)
使用:
#create index by column id
df = df.set_index('id')
#convert columns to datetimes and shift startTime per groups
df['startTime'] = pd.to_datetime(df['startTime']).groupby(level=0).shift(-1)
df['endTime'] = pd.to_datetime(df['endTime'])
#subtract, remove first missing values per groups and set column from index
df = df['startTime'].sub(df['endTime']).dropna().reset_index(name='time')
print (df)
id time
0 1 00:04:20
1 2 00:11:21
2 2 04:21:32