I want to populate some pandas data frames in an existing file . I followed these instructions:
How to write to an existing excel file without overwriting data (using pandas)? via:
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()
However, existing sheets will be deleted, an “approximate” sheet is created and only df will be integrated in a specific place. What have I done wrong? I want "data_df" to be written to an existing excel file in an existing "example" sheet, preserving other sheets and data.
thank
Df example:
data_df=pd.DataFrame(np.arange(12).reshape((2, 6)), index=["Time","Value"])
source
share