Python Pandas ExcelWriter,错误值消失了

时间:2018-11-26 22:09:58

标签: python excel

附带的代码将打开一个Excel工作表,检索数据,对其进行处理,然后将结果写入另一个电子表格。 奇怪的是,它第一次起作用,但是第二次出现问题。而且我不知道如何解决该问题。 Python不会给我任何错误。

程序将不同的总数相加,然后根据商品写出所售商品的数量。

第二次,我们看到TOB CB,现金或支票值消失了,我不明白为什么。

我第一次运行代码: Print data

我重新启动代码: second print data

我们看到总计值消失了,我们看到总计值消失了。当我查看工作表excel 1时,我们注意到€消失了。这可能是问题的根源。但是我不明白为什么迹象消失了。

Excel之前的代码: excel original

运行后的Excel: after running

这是Excel文件:https://drive.google.com/file/d/1LtDvot78Uh-e8MhiG1Ti-MLc05aXyGua/view?usp=sharing

提示复制了excel文件

目标是能够多次运行代码而不会出现此问题

谢谢!!!!

import pandas as pd
import numpy as np
import xlsxwriter
from openpyxl import load_workbook
from pandas import ExcelWriter
from pandas import ExcelFile
data=pd.read_excel("site.xlsx","Feuille1", keep_default_na=False, skiprows = [0,1]) 
print(data)

totalballon=[]
b=[]
espece=np.where(data['Unnamed: 2']=='TOTAL Espèces')[0]
carte=np.where(data['Unnamed: 2']=='TOTAL CB')[0]
cheque=np.where(data['Unnamed: 2']=='TOTAL Chèque')[0]

articles=["Ballon bleu", "CH24 jaune", "Ballon rouge", "Chapeau", "Ballon vert", "Pantalon", "Fleurs"]

print(np.sum(data['Unnamed: 4'][espece]))
print(np.sum(data['Unnamed: 4'][carte]))
print(np.sum(data['Unnamed: 4'][cheque]))

for i in articles:
    nb=np.where(data['Unnamed: 2']==i)[0]
    b.append(np.sum(data['Unnamed: 1'][nb]))
print(b)

ballon=["Ballon bleu", "Ballon rouge", "Ballon vert"]

for j in ballon:
    nb=np.where(data['Unnamed: 2']==j)[0]
    totalballon.append(np.sum(data['Unnamed: 1'][nb]))
totalballon=[sum(totalballon)]
print(totalballon)

with pd.ExcelWriter('site.xlsx', engine='openpyxl') as writer:
    writer.book = load_workbook('site.xlsx')

    TOTALespece = np.sum(data['Unnamed: 4'][espece])
    TOTALcarte = np.sum(data['Unnamed: 4'][carte])
    TOTALcheque = np.sum(data['Unnamed: 4'][cheque])

    df1 = pd.DataFrame({'articles':articles})
    df2 = pd.DataFrame({'nombres':b})
    df3 = pd.DataFrame({'Total ballon':totalballon})

    dfe = pd.DataFrame({'TOTAL Espèces':[TOTALespece]})
    dfcarte = pd.DataFrame({'TOTAL CB':[TOTALcarte]})
    dfcheque = pd.DataFrame({'TOTAL Chèque':[TOTALcheque]})

    df1.to_excel(writer, "Feuille2", index=False)
    df2.to_excel(writer, "Feuille2", index=False, startcol=1)
    df3.to_excel(writer, "Feuille2", index=False, startcol=2)
    dfe.to_excel(writer, "Feuille2", index=False, startcol=3)
    dfcarte.to_excel(writer, "Feuille2", index=False, startcol=4)
    dfcheque.to_excel(writer, "Feuille2", index=False, startcol=5)

0 个答案:

没有答案