我的代码和输入:
import pandas as pd
df1 = pd.DataFrame({'frame':[1,2,3,4,5,6,7,8,9,10,11,12],
'label':['other','splenic','other','other','hepatic','other','other','other','hepatic','other','splenic','other']})
df2 = pd.DataFrame({'frame':[1,2,3,4,5,6,7,8,9,10,11,12],
'label':['other','heaptic','other','other','splenic','other','other','other','splenic','other','hepatic','other']})
label = ['other','hepatic','splenic']
for i in range(0,len(label)):
if label[i] not in 'other':
start_frame = df1.loc[(df1['label']=='splenic'),'frame']
end_frame = df1.loc[(df1['label']=='hepatic'),'frame']
else: print('other')
假设我想要在两个发生标签(splenic
和hepatic
)之间的掩码开始/结束帧进行进一步计算。我的问题是,在数据帧中,标签以不同的顺序出现。例如,在df1
中,splenic
首先出现在frame
=2处,因此它将是我的start_frame
。下一个我的end_frame
是当我出现时hepatic
,其中frame
=5。如果我们继续下一步,我的start_frame
将是hepatic
,其中frame
=9,end_frame
是splenic
,其中frame
=11。在df2中,顺序相反。对于哪个先出现,没有真正的模式。
所以我不能说splenic
将是我的“开始”hepatic
将是“结束”。它取决于哪个先出现:hepatic
或splenic
,所以它将是“开始”,第二个标签将分别是“结束”。
我对df1
的期望:
start_frame=[2,9]
end_frame=[5,11]
你可以这样试试
试试这个:
我建议每次出现一个目标标签:
现在我们可以提取事件的索引,并将each(index of index in
id_key
)设置为偶数start
,将each奇数设置为end
:正确的起始帧和结束帧为:
其结果是:
相关问题 更多 >
编程相关推荐