1############################################################################## 2# 3# An example of writing multiple dataframes to worksheets using Pandas and 4# XlsxWriter. 5# 6# Copyright 2013-2021, John McNamara, jmcnamara@cpan.org 7# 8 9import pandas as pd 10 11 12# Create some Pandas dataframes from some data. 13df1 = pd.DataFrame({'Data': [11, 12, 13, 14]}) 14df2 = pd.DataFrame({'Data': [21, 22, 23, 24]}) 15df3 = pd.DataFrame({'Data': [31, 32, 33, 34]}) 16 17# Create a Pandas Excel writer using XlsxWriter as the engine. 18writer = pd.ExcelWriter('pandas_multiple.xlsx', engine='xlsxwriter') 19 20# Write each dataframe to a different worksheet. 21df1.to_excel(writer, sheet_name='Sheet1') 22df2.to_excel(writer, sheet_name='Sheet2') 23df3.to_excel(writer, sheet_name='Sheet3') 24 25# Close the Pandas Excel writer and output the Excel file. 26writer.save() 27