Python-比较两个数据帧之间的范围

Python-比较两个数据帧之间的范围,python,pandas,Python,Pandas,对于df1: Country fruit low high 0 Spain orange 100 20000 1 Italy apple 500 50000 2 Aus grape 300 10000 和df2: City fruit low high 0 sample1 orange 50 200 1 sample1 apple 10 400 2 sample2 orange 25000 5000

对于df1:

    Country fruit   low high
0   Spain   orange  100 20000
1   Italy   apple   500 50000
2   Aus     grape   300 10000
和df2:

    City    fruit   low high
0   sample1 orange  50  200
1   sample1 apple   10  400
2   sample2 orange  25000   50000
3   sample3 orange  50  300
4   sample3 grape   350 1000
5   sample3 grape   10  100
如果df2中介于“低”和“高”之间的范围包含在df1中的“低”和“高”范围内,我希望根据“水果”匹配行,并从df1中提取行。因此,预期产出将是:

    City    fruit   low high  Country   fruit   low high
0   sample1 orange  50  200   Spain     orange  100 20000
1   sample3 orange  50  300   Spain     orange  100 20000
2   sample3 grape   350 1000  Aus       grape   300 10000
我想它可能会像这样开始:

for sample, subdf in df2.groupby("fruit"):        
        for index, row in subdf.iterrows():
通过以下方式与外部联接和筛选一起使用:

df1=df2.merge(df1,on='fruit',how='outer',后缀=('','1'))
df2=df1[(df1.low1=df1.low)]
打印(df2)
城市水果低高国家低1高1
0样本1橙色50 200西班牙100 20000
2个样本3橙色50 300西班牙100 20000
4个样本3葡萄350 1000澳元300 10000澳元

我会使用左连接而不是外部连接

>>> (
    df2
    .merge(df1, how='left', on='fruit', suffixes=('', '_country'))
    .loc[lambda frame: frame.eval('(low > low_country) and (high < high_country)')]
    .reset_index()
    )
   index     City  fruit  low  high Country  low_country  high_country
0      4  sample3  grape  350  1000     Aus          300         10000
>(
df2
.merge(df1,how='left',on='fruit',后缀=('','u country'))
.loc[lambda帧:帧.eval('(低>低_国家)和(高<高_国家)]
.reset_index()
)
指数城市水果低高国家低国家高国家
0 4样本3葡萄350 1000澳元300 10000澳元

预期输出的前两行不在范围之外吗?例如,在第一行中,
df2
中50的低位不包含在
df1
low
high
之间(分别为100和20000)。感谢您的解决方案,Alexander,我说“包含在”是错误的,因为如果存在部分重叠,我会感到满意。然而,我认为您的解决方案在某些时候也会派上用场
>>> (
    df2
    .merge(df1, how='left', on='fruit', suffixes=('', '_country'))
    .loc[lambda frame: frame.eval('(low > low_country) and (high < high_country)')]
    .reset_index()
    )
   index     City  fruit  low  high Country  low_country  high_country
0      4  sample3  grape  350  1000     Aus          300         10000