How to Write to an Excel file with Pandas

01.05.2021

Intro

Excel files are utilized in many businesses. It is often a request to modify some data, then present a report in Excel format to managers. In this article, we will show you how to turn your dataframes into an Excel file with pandas.

Creating an Excel file with Pandas

For Exel 2007 and later, you will need the openpyxl library. We can install that using the following pip command.

pip install openpyxl

To create an Excel file from your dataframe, we call the to_excel method and specify a sheet_name parameter. this will write a local file that we can open with Excel.

import pandas as pd


df = pd.DataFrame([
	{
		"person": "James",
		"sales": 1000,
	},
	{
		"person": "Clara",
		"sales": 3000,
	}
])

df.to_excel('data.xlsx', sheet_name='sheet1')