python - Fastest way to merge pandas dataframe on ranges -


i have dataframe a

    ip_address 0   13 1   5 2   20 3   11 .. ........ 

and dataframe b

    lowerbound_ip_address   upperbound_ip_address           country 0    0                       10                             australia 1    11                      20                             china 

based on need add column in a such that

ip_address  country 13          china 5           australia 

i have idea should write define function , call map on each row of a. how search through each row of b this. there better way this.

use pd.intervalindex

in [2503]: s = pd.intervalindex.from_arrays(dfb.lowerbound_ip_address,                                             dfb.upperbound_ip_address, 'both')  in [2504]: dfa.assign(country=dfb.set_index(s).loc[dfa.ip_address].country.values) out[2504]:    ip_address    country 0          13      china 1           5  australia 2          20      china 3          11      china 

details

in [2505]: s out[2505]: intervalindex([[0, 10], [11, 20]]               closed='both',               dtype='interval[int64]')  in [2507]: dfb.set_index(s) out[2507]:           lowerbound_ip_address  upperbound_ip_address    country [0, 10]                       0                     10  australia [11, 20]                     11                     20      china  in [2506]: dfb.set_index(s).loc[dfa.ip_address] out[2506]:           lowerbound_ip_address  upperbound_ip_address    country [11, 20]                     11                     20      china [0, 10]                       0                     10  australia [11, 20]                     11                     20      china [11, 20]                     11                     20      china 

setup

in [2508]: dfa out[2508]:    ip_address 0          13 1           5 2          20 3          11  in [2509]: dfb out[2509]:    lowerbound_ip_address  upperbound_ip_address    country 0                      0                     10  australia 1                     11                     20      china 

Comments

Popular posts from this blog

neo4j - finding mutual friends in a cypher statement starting with three or more persons -

php - How to remove letter in front of the word laravel -

minify - Minimizing css files -