在范围上合并pandas数据框的最快方法

12

我有一个数据框 A

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

以及另一个数据框 B

    lowerbound_ip_address   upperbound_ip_address           country
0    0                       10                             Australia
1    11                      20                             China

基于此,我需要在 A 中添加一列,以便

ip_address  country
13          China
5           Australia

我有一个想法,应该编写一个函数,并在A的每一行上调用map。但是我该如何搜索B的每一行以进行此操作?有更好的方法吗?


下限和上限是以固定间隔为10吗? - ShreyasG
@ShreyasG 不,它们没有固定的。 - John Constantine
3个回答

19

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

详细信息

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

安装设置

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

很好地使用了IntervalIndex;想要在文档中添加一个像这样的小例子吗? - Jeff
1
如果 dfa.ip_address 包含了 dfb 的任何索引边界未覆盖的项,会出现 loc KeyError。您会如何处理这种情况? - xicocaio

5
尝试使用 pd.merge_asof 进行合并。
df['lowerbound_ip_address']=df['ip_address']
pd.merge_asof(df1,df,on='lowerbound_ip_address',direction ='forward',allow_exact_matches =False)
Out[811]: 
   lowerbound_ip_address  upperbound_ip_address    country  ip_address
0                      0                     10  Australia           5
1                     11                     20      China          13

当输入的数据框很大时,这非常快。但需要注意的是索引列需要排序。 - Alex

4

IntervalIndex是pandas 0.20.0版本以后引入的,@JohnGalt提供的解决方案非常出色。

在此版本之前,可以使用以下解决方案,将IP地址按国家扩展到完整范围。

df_ip = pd.concat([pd.DataFrame(
    {'ip_address': range(row['lowerbound_ip_address'], row['upperbound_ip_address'] + 1), 
     'country': row['country']}) 
    for _, row in dfb.iterrows()]).set_index('ip_address')
>>> dfa.set_index('ip_address').join(df_ip)
              country
ip_address           
13              China
5           Australia
20              China
11              China

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接