0

Colleagues, I have the following excel file. But, when I compile the program again, my data is overwritten. I'd like to put each results of the next compilations in a row. I've seen some examples with append, but I can not make it work. Could someone help me, please? !!

writer = pd.ExcelWriter('output.xlsx')

df = pd.DataFrame({'Function':[Output],'Circuit':[gates],'Cost':[cost]},)

df.to_excel(writer,'Sheet1')

writer.save()
3
  • Append to other columns or worksheets? E.g.: stackoverflow.com/questions/20219254/… Commented Aug 1, 2017 at 8:29
  • 1
    @albert append to other rows. Commented Aug 1, 2017 at 8:38
  • Probably not the most efficient, but you can read the excel file into a df, append new data into the df, and write the df into a new excel. Commented Aug 1, 2017 at 9:17

1 Answer 1

3

I guess there is no direct way to append rows to existing excel

Try this code, it may help

read your excel as dataframe

df1 = pd.read_excel('output.xlsx')

and your defined dataframe

df2 = pd.DataFrame({'Function':[Output],'Circuit':[gates],'Cost':[cost]})

then write both dataframe to excel

df1.to_excel(writer,startrow=0,index=False)
df2.to_excel(writer,startrow=len(df1)+1,header=False,index=False)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you!!! It's working!!! It's a little messy the columns, but no overwriting the data...
if my answer helped you, would you please accept this answer!
How I do that? I'm new here!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.