menu

Pandas write dataframes to multiple sheets in Excel

Do you generate multiple dataframes and write each one out to its own excel or csv file?

You can quickly generate a single excel with mutiple sheet with the below snippet

pip install xlsxwriter

You can also use openpyxl as the excel engine, but for multiple sheets, I will recommend using xlsxwriter

I assume you already have pandas installed

import pandas as pd

data = {'Name':['Tom', 'Brad', 'Kyle', 'Jerry'],
        'Age':[20, 21, 19, 18],
        'Height' : [6.1, 5.9, 6.0, 6.1]
        }

df = pd.DataFrame(data)

I will split the dataframe in 2 and save each part of the dataframe on a seperate sheet

import xlsxwriter

with pd.ExcelWriter('file path') as writer:
	df[:2].to_excel(writer, "Sheet_name_1", header=1, index=false, engine="xlsxwriter")
	df[2:].to_excel(writer, "Sheet_name_2", header=1, index=false, engine="xlsxwriter")
	writer.save()

For more details on the configuartion available on the to_excel pandas function refer to Pandas Documentation

Buy me a coffeeBuy me a coffee