[英]Filtering Python Pandas dataframe by IP octet
I have been Goog'ling this for a while, but haven't found the solution, so a hint in the right direction would be appreciated...我已经在谷歌上搜索了一段时间,但还没有找到解决方案,所以在正确方向上的提示将不胜感激......
I have a Pandas dataframe with roughly 13.000 rows with 96 columns.我有一个 Pandas dataframe 大约有 13.000 行和 96 列。 One of the columns contains IP addresses that I would like to filter on.其中一列包含我想过滤的 IP 地址。
I would like to remove all rows where the IP address matches these any of these: 10.x.220.x or 10.x.240.x我想删除 IP 地址与以下任何一个匹配的所有行:10.x.220.x 或 10.x.240.x
Use boolean indexing with a regex and str.fullmatch
:使用带有正则表达式和str.fullmatch
的boolean 索引:
df2 = df[~df['ip_column'].str.fullmatch(r'10\.\d+\.(220|240)\.\d+')]
Example input:示例输入:
col ip_column
0 A 10.1.220.1
1 B 10.0.0.1
2 C 10.127.240.0
3 D 127.0.0.1
Matching output:配套output:
col ip_column
1 B 10.0.0.1
3 D 127.0.0.1
regex:正则表达式:
10 # match 10
\. # match a dot
\d+ # match one or more digits
\. # match a dot
(220|240) # match 220 or 240
\. # match a dot
\d+ # match one or more digits
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.