根据您的数据,您可以将 end
列进行移位,并将其与 begin
进行比较:
groups = df.groupby('type')['end'].shift().ne(df['begin']).cumsum()
(df.groupby(['type', groups])
.agg({'begin':'first', 'end':'last'})
)
输出:
begin end
type
A 1 1 9
3 17 20
B 2 9 17