将 pandas 数据框附加到现有的 excel 表 [英] Append a pandas dataframe to an existing excel table

查看:92
本文介绍了将 pandas 数据框附加到现有的 excel 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要以下方面的帮助.我目前每天使用python pandas打开一个庞大的电子表格(这个电子表格是一个报告,因此电子表格里面的数据每天都不一样).Pandas 数据框允许我快速处理数据并生成最终输出表,数据比初始 excel 文件少得多.现在,在第 1 天,我需要将此输出数据框(3 行 10 列)添加到新的 Excel 工作表(假设为工作表 1).

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). Pandas dataframe allows me to quickly crunch the data and generate a final output table, with much less data than the initial excel file. 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).

在第 2 天,我需要获取数据框的新输出并将其附加到现有工作表 1.因此在第 2 天结束时,工作表 1 中的表将有 6 行和 10 列.

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.

第三天,同样的事情.我将启动我的 python pnadas 工具,从 excel 报告中读取数据,生成一个 3x10 的输出数据框并将其再次附加到我的 excel 文件中.

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.

我找不到附加到现有 Excel 表格的方法.有人可以帮忙吗?提前谢谢了,安德里亚

I can't find a way to append to an existing excel table. Could anybody help? Many thanks in advance, Andrea

推荐答案

如果你使用 openpyxl 的数据帧实用程序,那么您应该能够对现有工作簿执行所需的一切操作,假设它适合内存.

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.

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)

这篇关于将 pandas 数据框附加到现有的 excel 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆