简体   繁体   中英

Join two tables with a range criteria using Python Pandas

I have a similar problem to this simplified version:

The experiment result is saved in the excel sheet, and I processed the data using Python Pandas and converted them to DataFrames.

Two tables given below: Table_Race save in DataFrame race Table_standard save in 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
>>> 

I would like to check the distance for each player and get their title according to the standards:

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

For example: Jo from league 2 and has distance 15 which is in between [15,20). Note that it's not [10,15), hence he get title 'Silver'

The expected result as follows:

    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

I can achieved this using two loops which basically get each distance from Table_race and search for (l, d) from each row of race's (League, Distance)

Looking for condition:

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

But this method is O(N^2) which is too slow, as my data can easily go over 100,000 which takes hours to finish.

Any better solutions?

Still working on solution but here is something to start :

>>> 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 

Merge and Multiple conditions links for their tutorials and drawbacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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