简体   繁体   English

无法将Pandas Dataframe附加到现有的Excel工作表

[英]unable to append pandas Dataframe to existing excel sheet

I am quite new to Python/Pandas. 我对Python / Pandas很陌生。 I have a situation where I have to update an existing sheet with new data every week. 我有一种情况,我必须每周用新数据更新一个现有工作表。 this 'new' data is basically a processed data from raw csv files which are generated every week and I have already written a python code to generate this 'new' data which is basically a pandas Dataframe in my code. 这些“新”数据基本上是每周都会生成的原始csv文件中的经过处理的数据,我已经编写了python代码来生成“新”数据,该数据基本上就是我代码中的pandas Dataframe。 Now I want to append this Dataframe object to an existing sheet in my excel workbook. 现在,我想将此Dataframe对象附加到excel工作簿中的现有工作表中。 I am already using the below code to write the DF to the XL Workbook into a specific sheet. 我已经在使用下面的代码将DF工作表写入XL工作簿到特定的工作表中。

workbook_master=openpyxl.load_workbook('C:\Claro\Pre-Sales\E2E Optimization\Transport\Transport Network Dashboard.xlsx')

writer=pandas.ExcelWriter('C:\Claro\Pre-Sales\E2E Optimization\Transport\Transport Network Dashboard.xlsx',engine='openpyxl',mode='a')

df_latency.to_excel(writer,sheet_name='Latency',startrow=workbook_master['Latency'].max_row,startcol=0,header=False,index=False)

writer.save()
writer.close()

now the problem is when i run the code and open the excel file, instead of writing the dataframe to existing sheet 'Latency', the code creates a new sheet 'Latency1' and writes the Dataframe to it. 现在的问题是当我运行代码并打开excel文件时,代码没有将数据帧写入现有工作表“ Latency”,而是创建了一个新工作表“ Latency1”并将数据帧写入其中。 the contents and the positioning of the Dataframe is correct but I do not understand why the code is creating a new sheet 'Latency1' instead of writing the Dataframe into existing sheet 'Latency' 数据框的内容和位置正确,但是我不明白为什么代码会创建一个新的工作表“ Latency1”,而不是将数据框写入现有的工作表“ Latency”

will greatly appreciate any help here. 非常感谢您的帮助。

Thanks Faheem 谢谢法赫姆

By default, when ExcelWriter is instantiated, it assumes a new Empty Workbook with no Worksheets. 默认情况下,实例化ExcelWriter时,它将假定没有工作表的新Empty Workbook。

So when you try to write data into 'Latency', it creates a new blank Worksheet instead. 因此,当您尝试将数据写入“延迟”时,它将创建一个新的空白工作表。 In addition, the openpxyl library performs a check before writing to "avoid duplicate names" (see openpxyl docs : line 18 ), which numerically increment the sheet name to write to 'Latency1' instead. 另外, openpxyl库在写入“避免重复名称”之前执行检查(请参阅openpxyl docs:第18行 ),它以数字方式递增工作表名称以写入“ Latency1”。

To go around this problem, copy the existing Worksheets into the ExcelWriter.sheets attribute, after writer is created. 要解决此问题,请在创建writer之后将现有的工作表复制到ExcelWriter.sheets属性中。 Like this: 像这样:

writer.sheets = dict((ws.title, ws) for ws in workbook_master.worksheets)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM