我有如下所示的数据框
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