追加数据框以使用Pandas精益求精 [英] append dataframe to excel with pandas

查看:85
本文介绍了追加数据框以使用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熊猫将现有的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)

btw:对于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屋!

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