[英]How can I combine chronologically consecutive rows based on a condition in pandas?
給定您的數據,您可以移動end
列並將其與begin
進行比較:
groups = df.groupby('type')['end'].shift().ne(df['begin']).cumsum()
(df.groupby(['type', groups])
.agg({'begin':'first', 'end':'last'})
)
Output:
begin end
type
A 1 1 9
3 17 20
B 2 9 17
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.