3

I have two dataframes processed from the database. I need to export those data frames into excel(libreoffice calc) in two different sheets.

DF1:
    symbol    datetime    value
0    MOV    06:25:02    148767
1    TBI    06:25:02    267198
2    HY     06:25:02    56232
3    KAMN   06:25:02    2247    

DF2:
    symbol    datetime    value
0    MC     06:25:02    1098560
1    AIG    06:25:02    5952267
2    CHS    06:25:02    1879192
3    VRX    06:25:02    5502438

I have tried the following,

    print df1
    wr1 = pd.ExcelWriter('/home/suresh/Desktop/20151123/symbol.ods')
    df1.to_excel(wr1, 'Sheet1')
    print df2 
    df2.to_excel(wr1, 'Sheet2')

Now, not able to open the excel file.

1
  • 1
    please show your efforts, SO is not a code writing service Commented Nov 23, 2015 at 15:27

1 Answer 1

11

If passing an existing ExcelWriter object, then the sheet will be added to the existing workbook. This can be used to save different DataFrames to one workbook:

Source - in the end of webpage.

writer = pd.ExcelWriter('output.xlsx')
DF1.to_excel(writer,'Sheet1')
DF2.to_excel(writer,'Sheet2')
writer.save()
writer.close()
Sign up to request clarification or add additional context in comments.

1 Comment

Do not forget to add, writer.close(), even if there is a warning message like: UserWarning: Calling close() on already closed file. warn("Calling close() on already closed file.") This will prevent errors, when trying to open the file

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.