我需要按IP地址范围过滤 Pandas Dataframe。没有正则表达式是否可能?

Ex. From 61.245.160.0   To 61.245.175.255

最佳答案

字符串在python中是可排序的,因此您应该能够摆脱这种情况:

In [11]: '61.245.160.0' < '61.245.175.255'
Out[11]: True

bool 掩码:
In [12]: df[('61.245.160.0' < df.ip) & (df.ip < '61.245.175.255')]

或采取一个切片(如果ip是索引):
In [13]: df.loc['61.245.160.0':'61.245.175.255']

关于python - 按IP地址范围过滤Pandas DataFrame,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22979109/

10-12 20:05