将Pandas DataFrame追加到现有的Excel文档中

时间:2019-01-14 17:35:18

标签: python excel pandas xlsxwriter

对于每个https://github.com/pandas-dev/pandas/pull/21251/files/09e5b456e1af5cde55f18f903ab90c761643b05a,我们应该能够将DataFrame附加到新的XLSX工作表中。

根据文档,我尝试了以下操作:

>>> import pandas as pd
>>>                
... d1 = pd.DataFrame({"A":['Bob','Joe', 'Mark'], 
...                "B":['5', '10', '20']})
>>> d2 = pd.DataFrame({"A":['Jeffrey','Ann', 'Sue'], 
...                "B":['1', '2', '3']})
>>> 
>>> # Create XLSX document for ticker
... writer = pd.ExcelWriter('test.xlsx',engine='openpyxl')
>>> d1.to_excel(writer,sheet_name='d1')
>>> writer.save()
>>> 
>>> writer = pd.ExcelWriter('test.xlsx',engine='openpyxl', mode='a')
>>> d2.to_excel(writer,sheet_name='d2')
>>> writer.save()
>>> 
>>> pd.__version__
'0.23.4'     # Just updated this per a comment
>>> 
>>> 

结果是一个名为'test.xlsx'的工作簿,带有一个选项卡'd2'。

如何防止工作簿/工作表表单被覆盖?

3 个答案:

答案 0 :(得分:0)

您可以使用with

with pd.ExcelWriter('test.xlsx', engine='openpyxl', mode='a') as writer:
    d1.to_excel(writer,sheet_name='d1')
    d2.to_excel(writer,sheet_name='d2')
    writer.save()

writer.close()

更新

这应该起作用,只是要注意必须先创建一个空白文件。您可以根据需要使用python创建一个空白文件。我创建了一个简单的循环,以某种方式模仿您要完成的工作的本质:

import pandas as pd
from openpyxl import load_workbook

d1 = pd.DataFrame({"A":['Bob','Joe', 'Mark'], 
               "B":['5', '10', '20']})
d2 = pd.DataFrame({"A":['Jeffrey','Ann', 'Sue'], 
                "B":['1', '2', '3']})

dfs = [d1,d2]

for i in range(len(dfs)):
    sheet = 'd'+str(i+1)
    data = dfs[i]
    writer = pd.ExcelWriter('atest.xlsx',engine='openpyxl', mode='a')
    writer.book = load_workbook('atest.xlsx') # here is the difference
    data.to_excel(writer,sheet_name=sheet)
    writer.save()
    writer.close()

或者这是修改后的第一个示例:

d1 = pd.DataFrame({"A":['Bob','Joe', 'Mark'], 
               "B":['5', '10', '20']})
d2 = pd.DataFrame({"A":['Jeffrey','Ann', 'Sue'], 
                "B":['1', '2', '3']})

writer = pd.ExcelWriter('atest.xlsx', engine='openpyxl', mode='w')
d1.to_excel(writer,sheet_name='d1')
writer.save()
writer.close()

writer = pd.ExcelWriter('atest.xlsx', engine='openpyxl', mode='a')
writer.book = load_workbook('atest.xlsx')
d2.to_excel(writer,sheet_name='d2')
writer.save()
writer.close()

答案 1 :(得分:0)

我在GitHub上发表了一篇文章,并收到了贡献者的回复(请参见下面的突出显示部分)。事实证明,此功能要到0.24之后才能发布,因此在0.23.1中不可用。仅供参考-我下载了RC,并成功试用了mode='a'选项。但是,可能存在不存在的工作簿错误。我收到FileNotFoundError: [Errno 2] No such file or directory: 'test.xlsx'

  
    
      

“此功能已作为0.24的一部分发布,过去几天我们刚刚发布了该版本的候选版本。您可以尝试使用RC或在此处使用master,如果按照贡献指南都无法解决问题,则可以尝试预计该版本不能在该版本之前的版本中运行。

    
  

答案 2 :(得分:0)

将熊猫作为pd导入

writer = pd.ExcelWriter(wk_path + save_file)
# ....
# build sc_files DataFrame and save. sc_files includes
# a column called OS.

sc_file.to_excel(writer, sheet_name='test')

# build data frame of OS counts out of sc_file
counts_os = sc_file.OS.value_counts() 

# To append to 'test' sheet, use startcol=x1, startrow=y
# To append counts_OS to the end of the current 'test' sheet
y = len(sc_file)
y += 1
counts_os.to_excel(writer, sheet_name='test', 
    startcol=1, startrow=y)

# write counts_os to sheet test2 
counts_os.to_excel(writer, sheet_name='test2')
writer.save()
writer.close()