Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Filter Pandas DataFrame by ip address range

I need to filter a pandas Dataframe by the range of ip addresses. Is it possible with out regular expressions?

Ex. From 61.245.160.0   To 61.245.175.255
like image 403
Nilani Algiriyage Avatar asked Apr 10 '14 05:04

Nilani Algiriyage


1 Answers

Strings are orderable in python, so you should be able to get away with just that:

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

Either boolean mask:

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

or take a slice (if ip were the index):

In [13]: df.loc['61.245.160.0':'61.245.175.255']
like image 177
Andy Hayden Avatar answered Sep 27 '22 18:09

Andy Hayden