Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Export many small DataFrames to a single Excel worksheet

With this code, it is possible to export every data frame in a new worksheet iterating data frames list:

def save_xls(list_dfs, xls_path):
    writer = ExcelWriter(xls_path)
    for n, df in enumerate(list_dfs):
        df.to_excel(writer,'sheet_dati%s' % n)
    writer.save()
save_xls(list_dfs, xls_path)

But its possible to export two or more data frames in a single worksheet?

like image 206
Manuel Zompetta Avatar asked Jan 09 '13 13:01

Manuel Zompetta


1 Answers

Something like this: ?

from pandas import ExcelWriter
def dfs2xlsx(list_dfs,xls_path = None):
    #save_xls([df1,df2],'output1.xlsx')
    if xls_path == None :
        xls_path = '~tmp.xlsx'
    writer = ExcelWriter(xls_path)
    i=0
    for n, df in enumerate(list_dfs):
        df.to_excel(writer,'Sheet1',startcol=i,startrow =2)
        i+= len(df.columns) + 2
    writer.save()
    os.system('start excel.exe %s' %(writer.path ))
like image 192
ashkan Avatar answered Oct 28 '22 20:10

ashkan