将pd数据框填入现有的excel表(使用openpyxl v2.3.2) [英] Fill in pd data frame into existing excel sheet (using openpyxl v2.3.2)

查看:1091
本文介绍了将pd数据框填入现有的excel表(使用openpyxl v2.3.2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些熊猫数据框填入现有的 Excel文件。我遵循以下说明:
如何写入现有的excel文件而不覆盖数据(使用熊猫)?
使用:

I want to fill in some pandas data frames into an existing excel file. I followed the instructions in: How to write to an existing excel file without overwriting data (using pandas)? using:

  from openpyxl import load_workbook
  import pandas as pd
  import numpy as np

  book=load_workbook("excel_proc.xlsx")
  writer=pd.ExcelWriter("excel_proc.xlsx", engine="openpyxl")
  writer.book = book
  writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
  data_df.to_excel(writer, sheet_name="example", startrow=100, startcol=5, index=False)
  writer.save()

但是,现有的工作表将被删除,生成示例工作表,只有df被集成在定义的位置。我做错了什么?我想要将data_df写入现有的示例表中的Excel文件,保留其他工作表和数据。

However, the existing sheets will be deleted, the "example" sheet is generated and only the df is integrated at the defined location. What did I do wrong? I want the "data_df" written into the existing excel file in the existing "example" sheet, keeping the other sheets and data.

谢谢

示例df:

data_df=pd.DataFrame(np.arange(12).reshape((2, 6)), index=["Time","Value"])


推荐答案

我自己解决了这个问题。我意识到即使load_workbook无法加载我的文件。因此,我更新了openpyxl包(conda install openpyxl)。版本不工作是:v2.3.2 (python 35)。现在的版本是:v2.4.0。

I resolved the problem on my own. I realised that even load_workbook cannot load my file. Therefore, I updated the openpyxl package (conda install openpyxl). The version not working was : v2.3.2 (python 35). The version now working is: v2.4.0.

我真的不知道,如果是最后的原因。但是现在,这些优点已经被填写在定义的位置,并保存数据。

I do not really know, if it was the reason at the end. But now the excels are filled in the defined locations and the data is kept.

这篇关于将pd数据框填入现有的excel表(使用openpyxl v2.3.2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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