将数据框附加到 Pandas 中 [英] append dataframe to excel with pandas
问题描述
我希望将数据框附加到 excel
I desire to append dataframe to excel
这段代码几乎可以如愿以偿.虽然它不是每次都附加.我运行它并将数据框放入excel.但是每次我运行它时它都不会附加.我也听说 openpyxl 是 CPU 密集型的,但没有听说过很多 解决方法.
This code works nearly as desire. Though it does not append each time. I run it and it puts data-frame in excel. But each time I run it it does not append. I also hear openpyxl is cpu intensive but not hear of many workarounds.
import pandas
from openpyxl import load_workbook
book = load_workbook('C:\OCC.xlsx')
writer = pandas.ExcelWriter('C:\OCC.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
df1.to_excel(writer, index = False)
writer.save()
我希望每次运行时都附加数据,这不会发生.
I want the data to append each time I run it, this is not happening.
数据输出看起来像原始数据:
Data output looks like original data:
A B C
H H H
我想再跑一次
A B C
H H H
H H H
如果这很明显,我很抱歉我是 Python 新手,并且我练习的示例没有按预期工作.
Apologies if this is obvious I new to python and examples I practise did not work as wanted.
问题是 - 我如何在每次运行时附加数据.我尝试更改为 xlsxwriter 但得到 AttributeError: 'Workbook' object has no attribute 'add_format'
Question is - how can I append data each time I run. I try change to xlsxwriter but get AttributeError: 'Workbook' object has no attribute 'add_format'
推荐答案
首先,这篇文章是解决方案的第一部分,你应该在其中指定startrow=
:使用 python pandas 将现有的 excel 表附加到新的数据框
first of all, this post is the first piece of the solution, where you should specify startrow=
:
Append existing excel sheet with new dataframe using python pandas
您也可以考虑 header=False
.所以它应该看起来像:
you might also consider header=False
.
so it should look like:
df1.to_excel(writer, startrow = 2,index = False, Header = False)
如果您希望它自动到达工作表的末尾并附加您的 df,请使用:
if you want it to automatically get to the end of the sheet and append your df then use:
startrow = writer.sheets['Sheet1'].max_row
如果您希望它遍历工作簿中的所有工作表:
and if you want it to go over all of the sheets in the workbook:
for sheetname in writer.sheets:
df1.to_excel(writer,sheet_name=sheetname, startrow=writer.sheets[sheetname].max_row, index = False,header= False)
顺便说一句:对于 writer.sheets
你可以使用字典理解(我认为它更干净,但这取决于你,它产生相同的输出):
btw: for the writer.sheets
you could use dictionary comprehension (I think it's more clean, but that's up to you, it produces the same output):
writer.sheets = {ws.title: ws for ws in book.worksheets}
所以完整的代码将是:
import pandas
from openpyxl import load_workbook
book = load_workbook('test.xlsx')
writer = pandas.ExcelWriter('test.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = {ws.title: ws for ws in book.worksheets}
for sheetname in writer.sheets:
df1.to_excel(writer,sheet_name=sheetname, startrow=writer.sheets[sheetname].max_row, index = False,header= False)
writer.save()
这篇关于将数据框附加到 Pandas 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!