pandas数据处理-筛选数据

文章描述了一个关于如何通过Python脚本,利用pandas库处理大量云主机数据,找出未被使用的主机,并将其与人员信息进行匹配,以减少资源浪费的过程。作者通过读取文本文件、提取IP地址,然后与Excel表格中的数据进行筛选和整合,最终生成新的数据表格。
摘要由CSDN通过智能技术生成

创作灵感:公司有几千台云主机,分配给公司几百个业务人员使用,但是呢有些人申请了他不用,导致资源严重浪费。我们从后台拉一下数据,有4百多台云主机未使用,我们要从我们登记的总表把人员部门名字和IP对应上。一个一个的搜的话,估计要几个小时,而且累手啊。因此有了一下创作灵感。
 

import pandas
import re

# 读取txt文件数据,保存到ip_txt_list列表
ip_txt_list = []
ip_list = []
password_list = []
path1 = r'C:\Users\Administrator\Desktop\虚拟机ip地址.txt'
with open(path1, 'rb') as r1:
    red_list = r1.readlines()

# 处理读取的txt文件数据,保存到ip_txt_list列表
for i in red_list:
    rstr = i.decode()
    r1 = rstr.strip()
    ip_txt_list.append(r1)

# 提取整理ip_txt_list列表中数据,保存到ip_list列表
for i in ip_txt_list:
    if i != '':
        ip1 = re.match('^10[.]18[.][0-9]{,3}[.][0-9]{,3}', i).group()
        ip_list.append(ip1)

# 读取表格
filename = r'C:\Users\Administrator\Desktop\虚拟机ip-1.xlsx'
df = pandas.read_excel(filename, sheet_name=0, usecols=['ip', '部门', '使用人'])
# 确认原始数据中不包含的IP数据,单独列出
screening_ip = []
not_include_ip = []

load_ip = list(df.get('ip'))
for i in ip_list:
    if i in load_ip:
        print(i)
        ip_index = ip_list.index(i)
        screening_ip.append(i)
    else:
        not_include_ip.append(i)

# 数据筛选
screening_df = df[df['ip'].isin(screening_ip)]
# 创建DataFrame对象
new_row = pandas.DataFrame({'ip地址': not_include_ip})
# 保存筛选数据,生成新的数据表格
with pandas.ExcelWriter(r'C:\Users\Administrator\Desktop\虚拟机ip-2.xlsx', engine='openpyxl', mode='w') as w:
    screening_df.to_excel(w, index=False, sheet_name='未登录企微ip')
    new_row.to_excel(w, index=False, sheet_name='未登记ip')
    print('保存成功')

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值