基于pandas的快递单处理
程序员文章站
2022-03-10 09:50:06
...
import numpy as np
import pandas as pd
goods_data=pd.read_excel("D:/input/数据.xlsx")#使用pandas读取数据
goods_data=goods_data.fillna(0)
#对表格进行处理
#第一步筛选出快递员取出的单号(完成)
data_1=goods_data["运单号"][goods_data.扫描类型=="快递员取出"]
# 保存下来保存到excle文件中
data_1= data_1.apply(lambda x: '%d' % x).astype(object)
writer=pd.ExcelWriter("D:/output/快递员取出的取出单号——单号列表.xlsx")
data_1.to_excel(writer,index=False,encoding='utf-8',sheet_name="Sheet1")
writer.save()
#第二步筛选
danhaos=goods_data.运单号.value_counts()
deal=[]
for i in range(len(danhaos)):
data2=goods_data[goods_data.运单号==danhaos.index[i]]
yizhandai2=data2[(data2.扫描类型=="驿站代收")]
# print(yizhandai)
# print(danhaos.index[i])
if len(yizhandai2)==2:
time2=goods_data["上传时间"][(goods_data.运单号==danhaos.index[i])&(goods_data.扫描类型=="驿站代收")]
time2=list(time2)
if time2[0].split(" ")[0]!=time2[1]:
deal.append([danhaos.index[i],"驿站代收"])
# 第三步筛选
deal2=[]
j=0
de=[]
for i in range(len(danhaos)):
sdata3=goods_data[goods_data.运单号==danhaos.index[i]]
yiandqianall=sdata3[(sdata3.扫描类型=="驿站代收")| (sdata3.扫描类型=="签收")]
sum1=len(yiandqianall)#统计总量
yizhandai=sdata3[sdata3.扫描类型=="驿站代收"]
yizhanqian=sdata3[sdata3.扫描类型=="签收"]
sumdai=len(yizhandai)
sumqian=len(yizhanqian)
if sumdai==1&sumqian==1:
time30=yizhandai.上传时间.values[0].split(" ")[0]
time31=yizhanqian.上传时间.values[0].split(" ")[0]
if time30!=time31:
deal2.append([danhaos.index[i],"驿站代收","签收"])
writer2=pd.ExcelWriter("D:/output/两个驿站代收但时间不一样的列表.xlsx")
for i in range(len(deal)):
all1=goods_data[(goods_data.运单号==deal[i][0])&(goods_data.扫描类型==deal[i][1])]
de.append(all1.index[0])
all1['运单号']= all1.apply(lambda x: '%d' % (x['运单号']),axis=1).astype(object)
all1.to_excel(writer2,index=False,encoding='utf-8',sheet_name="Sheet1")
writer2.save()
all2=[]
for i in range(len(deal2)):
all0=goods_data[(goods_data.运单号==deal2[i][0])&(goods_data.扫描类型==deal2[i][1])]
all2.append(all0.values[0])
de.append(all0.index[0])
all1=goods_data[(goods_data.运单号==deal2[i][0])&(goods_data.扫描类型==deal2[i][2])]
all2.append(all1.values[0])
de.append(all1.index[0])
writer3=pd.ExcelWriter("D:/output/驿站代收和签收时间不一样的.xlsx")
df = pd.DataFrame(all2, columns=goods_data.columns)
df['运单号']= df.apply(lambda x: '%d' % (x['运单号']),axis=1).astype(object)
df.to_excel(writer3,index=False,encoding='utf-8',sheet_name="Sheet1")
writer3.save()
newgoods=goods_data.drop(index=de)#最后删除这些挑选出来的数据
writer4=pd.ExcelWriter("D:/output/处理后的表.xlsx")
newgoods['运单号']= newgoods.apply(lambda x: '%d' % (x['运单号']),axis=1).astype(object)
newgoods['袋号']= newgoods.apply(lambda x: '%d' % (x['袋号']),axis=1).astype(object)
newgoods.to_excel(writer4,index=False,encoding='utf-8',sheet_name="Sheet1")
writer4.save()
import numpy as np
import pandas as pd
import os
names=os.listdir("D:/input/")
filname="D:/input/"+names[0]
print(filname)
danhaos=goods_data.运单号.value_counts()
goods_data=pd.read_excel(filname)#使用pandas读取数据
# goods_data=goods_data.fillna(0)
print(danhaos)
deal3=[]
sdata3=goods_data[goods_data.运单号==danhaos.index[11]]
yizhandai=sdata3[sdata3.扫描类型=="驿站代收"]
yizhanqian=sdata3[sdata3.扫描类型=="签收"]
data3=yizhanqian.扫描类型.value_counts()
len(yizhanqian)
data2=goods_data[goods_data.运单号==danhaos.index[10]]
yizhandai=data2[(data2.扫描类型=="驿站代收")]
yizhandai.上传时间.values[0].split(" ")[0]
deal3[0]
sdata3=goods_data[goods_data.运单号==danhaos.index[5]]
yiandqianall=sdata3[(sdata3.扫描类型=="驿站代收")| (sdata3.扫描类型=="签收")]
sum1=len(yiandqianall)#统计总量
data_2_3=yiandqianall.扫描类型.value_counts()
yizhandai=sdata3[sdata3.扫描类型=="驿站代收"]
yizhanqian=sdata3[sdata3.扫描类型=="签收"]
if len(yizhandai)==1&len(yizhanqian)==1:
print(yizhandai.上传时间.values[0]==yizhanqian.上传时间.values[0])
print(yizhanqian.上传时间.values[0])
else:
print("meiyou")