Python 添加列并将IP映射到具有第二个表的国家/地区数据框(查找)

Python 添加列并将IP映射到具有第二个表的国家/地区数据框(查找),python,pandas,Python,Pandas,我必须坐在桌子上。表1中有一列带有IP号。表2列出了不同国家的IP边界。我想在第一个表中添加一列,其中包含resp country 表:ip_国家 lower_bound_ip_address upper_bound_ip_address country 0 16777216 16777471 Australia 1 16777472 16777727

我必须坐在桌子上。表1中有一列带有IP号。表2列出了不同国家的IP边界。我想在第一个表中添加一列,其中包含resp country

表:ip_国家

        lower_bound_ip_address  upper_bound_ip_address  country
0       16777216               16777471                 Australia
1       16777472               16777727                 China
2       16777728               16778239                 China
3       16778240               16779263                 Australia
4       16779264               16781311                 China
表2有一列ip_地址,我想添加一列'country',并从表1中查找值

这是我试过的。 这是行不通的

#map ip to country
def ip_to_country(ip):
    country = ip_country.loc[(ip_country['lower_bound_ip_address'] <= ip) & (ip <= ip_country['upper_bound_ip_address']), 'country'].item()
    return country

X["country"] = X["ip_address"].apply(ip_to_country)
#将ip映射到国家/地区
def ip_至_国家/地区(ip):

country=ip_country.loc[(ip_country[“下限ip_地址”]你不希望IP>=下限吗?这可能会导致它返回多个可能性。我确实得到了下限。不过,基于值错误,现在是有意义的,似乎存在不返回结果的情况。如果要使用函数,如果函数中没有IP,它可能会返回默认值边界。如果没有其他国家,返回国家——没有解决它。这是你的意思吗?