我有以下形式熊貓:伯爵在一組時間間隔的交叉口
import pandas as pd
Out[1]:
df = pd.DataFrame({'id':[1,2,3,4,5],
'group':['A','A','A','B','B'],
'start':['2012-08-19','2012-08-22','2013-08-19','2012-08-19','2013-08-19'],
'end':['2012-08-28','2013-09-13','2013-08-19','2012-12-19','2014-08-19']})
id group start end
0 1 A 2012-08-19 2012-08-28
1 2 A 2012-08-22 2013-09-13
2 3 A 2013-08-19 2013-08-21
3 4 B 2012-08-19 2012-12-19
4 5 B 2013-08-19 2014-08-19
的數據幀對於我的數據框定的行,我想數一下那個有同組的項目數重疊的時間間隔。
例如,在2012年8月22日至2013年9月13日的A組id 2範圍內,因此id 1(2012年8月19日至2012年8月28日)以及id 3(2013年8月19日至2013年8月21日) 2.
計數相反還有在B組
所以我的例子數據幀中的項目之間沒有重疊上述,我想產生類似
Out[2]:
id group start end count
0 1 A 2012-08-19 2012-08-28 1
1 2 A 2012-08-22 2013-09-13 2
2 3 A 2013-08-19 2013-08-21 1
3 4 B 2012-08-19 2012-12-19 0
4 5 B 2013-08-19 2014-08-19 0
我可以「brute-強迫「這一點,但我想知道是否有更高效的熊貓這樣做的方式。
在此先感謝您的幫助
你能詳細一點關於「相交時間間隔」。我的意思是說明你是如何計算的 – Dark
@Bharathshetty - 我已經詳細闡述了我的問題 – johnaphun