4 Pandas 处理excel复杂列转化为多行 2020080
2020-08-07 本文已影响0人
avyhlj
1 业务背景
1.png
2 解决方法
#导入包
import pandas as pd
import struct
import socket
#1 导入数据
data = pd.read_excel('./python_explode.xls',sheet_name ='Sheet1')
2.png
#2 计算两个IP段之间的有效IP地址
def findIPs(start, end):
ipstruct = struct.Struct('>I')
start, = ipstruct.unpack(socket.inet_aton(start))
end, = ipstruct.unpack(socket.inet_aton(end))
return [socket.inet_ntoa(ipstruct.pack(i)) for i in range(start, end+1)]
#print(findIPs('111.111.111.0', '111.111.111.3'))
data['ips'] =''
for i in range(len(data)):
data['ips'] [i] = findIPs(data['起始IP地址(*)'][i], data['终止IP地址(*)'][i])
#3 将ips复杂多列转多行
data_explode = data.explode('ips')
3 结果
3.png