简体   繁体   English

将熊猫数据框附加到现有的Excel表中

[英]Append a pandas dataframe to an existing excel table

I need some help with the following. 我需要以下帮助。 I currently use python pandas to open a massive spreadsheet every day (this spreadsheet is a report, hence every day the data inside the spreadsheet is different). 我目前每天都使用python pandas打开一个庞大的电子表格(此电子表格是报表,因此每天电子表格中的数据都不同)。 Pandas dataframe allows me to quickly crunch the data and generate a final output table, with much less data than the initial excel file. Pandas数据框使我可以快速处理数据并生成最终输出表,而数据要少于初始excel文件。 Now, on day 1, I would need to add this output dataframe (3 rows 10 columns) to a new excel sheet (let's say sheet 1). 现在,在第1天,我需要将此输出数据帧(3行10列)添加到新的Excel工作表(比如工作表1)。

On day 2, I would need to take the new output of the dataframe and append it to the existing sheet 1. So at the end of day 2, the table in sheet1 would have 6 rows and 10 columns. 在第2天,我需要获取数据框的新输出并将其附加到现有工作表1。因此,在第2天结束时,sheet1中的表将具有6行10列。

On day 3, same thing. 在第三天,同样的事情。 I will launch my python pnadas tool, read data from the excel report, generate an output dataframe 3x10 and append it again to my excel file. 我将启动我的python pnadas工具,从excel报告中读取数据,生成3x10的输出数据框,并将其再次附加到我的excel文件中。

I can't find a way to append to an existing excel table. 我找不到添加到现有excel表的方法。 Could anybody help? 有人可以帮忙吗? Many thanks in advance, Andrea 在此先感谢Andrea

If you use openpyxl's utilities for dataframes then you should be able to do everything you need with the existing workbook, assuming this fits into memory. 如果您将openpyxl的实用程序用于数据帧,那么您应该能够使用现有工作簿进行所需的一切, 前提是这适合内存。

from openpyxl import load_workbook
from openpyxl.utils.dataframe import dataframe_to_rows
wb = load_workbook("C:\Andrea\master_file.xlsx")
ws = wb[SHEETNAME]
for row in dataframe_to_rows(dt_today):
    ws.append(row)

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

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