如何比较熊猫/蟒蛇中的两行,如果它们重叠



我有如下所示的数据框

product        model   ci_low      ci_upp  
A             X    0.041667    48.0 
A             Y    0.000000    21.0 

我想检查ci_low和ci_upp是否按产品重叠并返回类似

product       CI Band X        CI Band Y         Overlap
A      [0.041667-48.0]   [0.00000 - 21.0]    Yes

注意:我有功能检查范围是否重叠,如果返回0,则它们不会重叠

def getOverlap(a, b):
return max(0, min(a[1], b[1]) - max(a[0], b[0]))

首先我们需要pivot并创建范围索引,然后使用overlap找到交集

df['Ci band']=pd.IntervalIndex.from_arrays(df.ci_low,df.ci_upp,closed='both')
s=df.pivot('product','model','Ci band').add_prefix('CI Band')
s['Overlap']=s.apply(lambda x : x['CI BandX'].overlaps(x['CI BandY']),axis=1)
s
Out[55]: 
model            CI BandX     CI BandY  Overlap
product                                        
A        [0.041667, 48.0]  [0.0, 21.0]     True

相关内容

  • 没有找到相关文章