繁体   English   中英

使用Python Pandas将具有范围条件的两个表联接起来

[英]Join two tables with a range criteria using Python Pandas

我有一个与此简化版本类似的问题:

实验结果保存在excel工作表中,我使用Python Pandas处理了数据并将其转换为DataFrames。

下面给出了两个表:Table_Race保存在DataFrame race中Table_standard保存在DataFrame std中

>>> data = [["Gold+",1,30,35],["Silver+",1,25,30],["Bronze+",1,20,25],["Gold",2,20,25],["Silver",2,15,20],["Bronze",2,10,15]]
>>> std = pd.DataFrame(data,columns=['Title','League','Start','End'])
>>> std
     Title  League  Start  End
0    Gold+       1     30   35
1  Silver+       1     25   30
2  Bronze+       1     20   25
3     Gold       2     20   25
4   Silver       2     15   20
5   Bronze       2     10   15
>>> data = [["John",1,26],["Ryan",1,33],["Mike",1,9],["Jo",2,15],["Riko",2,21],["Kiven",2,13]]
>>> race = pd.DataFrame(data,columns=['Name','League','Distance'])
>>> race
    Name  League  Distance
0   John       1        26
1   Ryan       1        33
2   Mike       1         9
3     Jo       2        21
4   Riko       2        15
5  Kiven       2        13
>>> 

我想检查每个球员的距离,并根据标准获得他们的头衔:

    Title <= distance in [start, end) and need to match league

例如:来自联赛2的乔,距离15在[15,20]之间。 请注意,它不是[10,15),因此他获得了标题“银”

预期结果如下:

    Name    League  Distance    Title
    John    1       26          Silver+
    Ryan    1       33          Gold+
    Mike    1       9           N/A
    Jo      2       21          Gold
    Riko    2       15          Silver
    Kiven   2       13          Bronze

我可以使用两个循环来实现此目的,这两个循环基本上从Table_race获取每个距离,并从种族的每一行(联赛,距离)中搜索(l,d)

寻找条件:

    l == League && d >= Start && d < End

但是此方法的速度为O(N ^ 2),它太慢了,因为我的数据很容易超过100,000,这需要数小时才能完成。

有更好的解决方案吗?

仍在研究解决方案,但这里是一些开始:

>>> data = [["Gold+",1,30,35],["Silver+",1,25,30],["Bronze+",1,20,25],["Gold",2,20,25],["Silver",2,15,20],["Bronze",2,10,15]]
>>> std = pd.DataFrame(data,columns=['Title','League','Start','End'])
>>> std
     Title  League  Start  End
0    Gold+       1     30   35
1  Silver+       1     25   30
2  Bronze+       1     20   25
3     Gold       2     20   25
4   Silver       2     15   20
5   Bronze       2     10   15

>>> data = [["John",1,26],["Ryan",1,33],["Mike",1,9],["Jo",2,21],["Riko",2,15],["Kiven",2,13]]
>>> race = pd.DataFrame(data,columns=['Name','League','Distance'])
>>> race
    Name  League  Distance
0   John       1        26
1   Ryan       1        33
2   Mike       1         9
3     Jo       2        21
4   Riko       2        15
5  Kiven       2        13
>>> result=pd.merge(race,std,on='League')
>>> result = result[(result.Distance >= result.Start)&(result.Distance < result.End)][["Name","League","Distance","Title"]]
>>> result
     Name  League  Distance    Title
1    John       1        26  Silver+
3    Ryan       1        33    Gold+
9      Jo       2        21     Gold
13   Riko       2        15   Silver
17  Kiven       2        13   Bronze 

合并多个条件链接,以了解其教程和缺点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM