【发布时间】:2021-11-21 17:15:18
【问题描述】:
我与多个送货和多个地址有关系。
我为每个区域(5 个区域)制作了一个数据透视表列表
在 jupyter notebook 中使用“for”,每个列表中的每个项目都显示为一个单独的数据透视表,一个在另一个之上,就像我需要的那样。
但是如何将它们保存在 5 张的 Excel 中?
我已经尝试了所有方法,只保存使用每个区域列表创建的最后一个数据透视,或者保存现有数据并删除所有数据。
我目前为每个区域创建了一个空电子表格,在 D 列第 1 行中只有一个标题。
expedition.xlsx(内有5张,北、东北、中西、东南、南)
当我尝试保存时,它最终会删除其他人并仅保留“北”
我设置了一个规则来识别D列是否有一个空单元格,如果它被填充,则通过向下跳过一行再试一次,如果它是空的,理论上它应该用列表的数据框填充。
如何将一个数据透视表填充到另一个下方?对于每个区域和列表项。
代码:https://pastebin.com/Cx3Zvf6D
import pandas as pd
import openpyxl
writer = pd.ExcelWriter("expedition.xlsx", engine='xlsxwriter')
# Creating the base sheet for each region, empty
pivot1 = pd.DataFrame({'Lista de Romaneio para Região Norte': [' ']})
pivot2 = pd.DataFrame({'Lista de Romaneio para Região Nordeste': [' ']})
pivot3 = pd.DataFrame({'Lista de Romaneio para Região Centro Oeste': ['']})
pivot4 = pd.DataFrame({'Lista de Romaneio para Região Sudeste': [' ']})
pivot5 = pd.DataFrame({'Lista de Romaneio para Região Sul': [' ']})
# creating a sheet in the spreadsheet for each region, with the title in column D, row 1
pivot1.to_excel(writer, sheet_name='Norte', index=False, startcol=3, freeze_panes=(1,0))
pivot2.to_excel(writer, sheet_name='Nordeste', index=False, startcol=3, freeze_panes=(1,0))
pivot3.to_excel(writer, sheet_name='Centro Oeste', index=False, startcol=3, freeze_panes=(1,0))
pivot4.to_excel(writer, sheet_name='Sudeste', index=False, startcol=3, freeze_panes=(1,0))
pivot5.to_excel(writer, sheet_name='Sul', index=False, startcol=3, freeze_panes=(1,0))
writer.close()
# List with the "keys" of each pivot table for each region
norte = ['PA_BEL', 'TO_PMW', 'AC_RBR']
nordeste = ['AL_MCZ', 'PB_JPA', 'BA_SSA', 'RN_NAT', 'PE_REC', 'CE_FOR', 'MA_IMP', 'MA_THE', 'PI_THE', 'BA_FEC']
centro_oeste = ['GO_GYN', 'DF_BSB', 'GO_BSB', 'MT_CGB', 'MS_CGR']
sudeste = ['ES_SRR', 'MG_BHZ', 'SP_PNM', 'SP_JDI', 'RJ_RIO', 'MG_UDI']
sul = ['RS_POA', 'PR_CWB', 'SC_CCM', 'RS_RIA', 'SC_FLN']
# example for the north (norte) region
if len(norte) > 0:
frete_expresso_norte = 0
for filial in norte:
# creating a pivot table for each flilial(key)
pivot1 = df[df.Filial_Transportador == filial].pivot_table(
index=['BU', 'Sold to Region', 'Filial_Transportador', 'Sold_to_Name', 'Sold to City', 'Delivery'],
values=['Quantidade','Volume', 'Palete', 'Net Value'], aggfunc='sum',
margins=True)
# reorders columns and renames ALL of pivot table to Total
ordem_das_colunas = ['Quantidade', 'Volume', 'Palete', 'Net Value']
pivot1 = pivot1[ordem_das_colunas].rename(index=dict(All='Total Romaneio'))
# creating subtotals and finding express shipping (if any)
total_palete_norte = pivot1.groupby('Filial_Transportador')['Palete'].sum()[1]
total_net_value_norte = pivot1.groupby('Filial_Transportador')['Net Value'].sum()[1]
# save the pivot table (pivot1) in excel in the north sheet where it is blank
# Here's where I want to put the code below saving the pivot table before starting the creation of the next one.
# code under construction
# after saving it continues normally
if total_palete_norte >= 29 or total_net_value_norte >= 2500000:
frete_expresso_norte = frete_expresso_norte + 1
else:
pass
display(pivot1)
else:
expedicao_norte = 'Não há volume para ser expedido à região Norte'
# Code under construction to insert inside the loop:
import openpyxl
# opening the spreadsheet with specific name
n = 0 # 0 = Norte / 1 = Nordeste / 2 = Centro Oeste / 3 = Sudeste / 4 = Sul
planilha_cx = openpyxl.load_workbook("Expedition.xlsx")
folhas = planilha_cx.sheetnames
folha = planilha_cx[folhas[n]]
# reading the cell
coluna = 4 # column D of the selected sheet
linha = 1 # start on the first line of the sheet
celula = folha.cell(linha, coluna).value
while celula != None: # looping while cell in column D is not blank
celula = folha.cell(linha, coluna).value # cell current value
if celula == None: # filling the cell if it is blank
linha = linha + 2
folha.cell(row=linha, column=1).value = 'aaaaaaa' # inserts the word 'aaaaa' but doesn't work with pivot1
planilha_cx.save("Expedition.xlsx")
break
else: # while cell D1 is not blank, add +1 to row
linha = linha + 1
pass
【问题讨论】:
标签: python excel pandas jupyter-notebook openpyxl