使用第二个表添加列并将IP映射到数据帧(查找)

2024-05-14 01:03:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我得坐在桌子上。表一有一列IP号。表二是不同国家的知识产权边界查找。我想在第一个表中添加一列resp country。你知道吗

表:ip\U国家

        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\ U地址,我想添加一列'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)

我确实得到了错误: ValueError:只能将大小为1的数组转换为Python标量

请帮忙。你知道吗


Tags: toipaddress知识产权国家upperlowerresp