如何写入一个现有的excel文件而不覆盖数据(使用 pandas )? [英] How to write to an existing excel file without overwriting data (using pandas)?
本文介绍了如何写入一个现有的excel文件而不覆盖数据(使用 pandas )?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我用大熊猫以下列方式写入excel文件:
I use pandas to write to excel file in the following fashion:
import pandas
writer = pandas.ExcelWriter('Masterfile.xlsx')
data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])
writer.save()
Masterfile.xlsx已经包含不同标签的数量。
Masterfile.xlsx already consists of number of different tabs.
熊猫正确写入主表,不幸的是它也会删除所有其他选项卡。
Pandas correctly writes to "Main" sheet, unfortunately it also deletes all other tabs.
推荐答案
p> Pandas docs表示它使用openpyxl for xlsx文件。快速浏览 ExcelWriter
中的代码,提供一些线索,可能会出现这样的情况:
Pandas docs says it uses openpyxl for xlsx files. Quick look through the code in ExcelWriter
gives a clue that something like this might work out:
import pandas
from openpyxl import load_workbook
book = load_workbook('Masterfile.xlsx')
writer = pandas.ExcelWriter('Masterfile.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])
writer.save()
这篇关于如何写入一个现有的excel文件而不覆盖数据(使用 pandas )?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文