2

I have an non-empty Excel-file where I want to insert a dataframe. What I tried:

import pandas as pd
import numpy as np
path = r'C:\Template_example.xlsx'
data = np.random.randn(5,10)
df = pd.DataFrame(data = data)
description = df.describe()
description.to_excel(path, sheet_name='Table1', startrow = 12, startcol = 2)

It exports the dataframe to the exact specified place. BUT: Everything else in the file is deleted. I want to keep everything. On the specified place, I have left empty rows and columns for that dataframe... How can I only "append" it to that place, in a way like copy and paste, without deleting everything else?

user2366975
  • 4,350
  • 9
  • 47
  • 87
  • possible duplicate of [How to write to an existing excel file without overwriting data?](http://stackoverflow.com/questions/20219254/how-to-write-to-an-existing-excel-file-without-overwriting-data) (when using openpyxl) – joris May 29 '14 at 13:56
  • It does not work. Error: `'Workbook' object has no attribute 'add_worksheet'` – user2366975 May 29 '14 at 14:37

1 Answers1

0

This works fine for me, appending the information to an excel sheet without deleting the previous content.

with pd.ExcelWriter('output.xlsx',
                mode='a') as writer:  
df.to_excel(writer, sheet_name='Table1',startrow = 12, startcol = 2)
simoncraf
  • 146
  • 5