我正在使用 netaddr python 库。我有 2 个数据帧,一个带有可转换为 CIDR 表示法的 IP 范围,另一个带有我想查看它们是否属于任何范围的 IP 地址。
创建范围数据框:
import pandas as pd
import netaddr
from netaddr import *
a = {'StartAddress': ['65.14.88.64', '148.77.37.88', '65.14.41.128', '65.14.40.0'],
'EndAddress': ['65.14.88.95', '148.77.37.95','65.14.41.135', '65.14.40.255']}
df1 = pd.DataFrame(data=a)
#Convert range to netaddr cidr format
def rangetocidr(row):
return netaddr.iprange_to_cidrs(row.StartAddress, row.EndAddress)
df1["CIDR"] = df1.apply(rangetocidr, axis=1)
df1
StartAddress EndAddress CIDR
0 65.14.88.64 65.14.88.95 [65.14.88.64/27]
1 148.77.37.88 148.77.37.95 [148.77.37.88/29]
2 65.14.41.128 65.14.41.135 [65.14.41.128/29]
3 65.14.40.0 65.14.40.255 [65.14.40.0/24]
df1["CIDR"].iloc[0]
[IPNetwork('65.14.88.64/27')]
创建 IP 数据帧:
b = {'IP': ['65.13.88.64', '148.65.37.88','65.14.88.65','148.77.37.93','66.15.41.132']}
df2 = pd.DataFrame(data=b)
#Convert ip to netaddr format
def iptonetaddrformat (row):
return netaddr.IPAddress(row.IP)
df2["IP_Format"] = df2.apply(iptonetaddrformat, axis=1)
df2
IP IP_Format
0 65.13.88.64 65.13.88.64
1 148.65.37.88 148.65.37.88
2 65.14.88.65 65.14.88.65
3 148.77.37.93 148.77.37.93
4 66.15.41.132 66.15.41.132
df2["IP_Format"].iloc[0]
IPAddress('65.13.88.64')
我期待中添加一列df2,如果IP地址是从CIDR块df1。所以它看起来像:
df2
IP IP_Format IN_CIDR
0 65.13.88.64 65.13.88.64 False
1 148.65.37.88 148.65.37.88 False
2 65.14.88.65 65.14.88.65 True
3 148.77.37.93 148.77.37.93 True
4 66.15.41.132 66.15.41.132 False
我更愿意仅使用 2 个数据帧中的列来执行此操作,但已通过将列转换为列表并使用以下内容进行了尝试,但这似乎不起作用:
df2list = repr(df2[['IP_Format']])
df1list = df[['CIDR']]
def ipincidr (row):
return netaddr.largest_matching_cidr(df2list, df1list)
df2['INRANGE'] = df2.apply(ipincidr, axis=1)
开满天机
相关分类