3
votes

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
please show your efforts, SO is not a code writing serviceEdChum

1 Answers

9
votes

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()