Pandas to Excel – MultiPage Workbook

For this task, I would like get data from my database & output to an Excel workbook. The first page of the workbook will contain the output from a query. The second page will contain aggregated data. In the following example, a Venue may have multiple rows in the data. In my aggregation, I want the count of distinct Venues by VenueType.

The following screenshot shows our starting point in the database; the first few records from our base table, a count of Venues by VenueType and then a count of distinct Venues by VenueType.

In the next screenshot, we see the same data in a Jupyter notebook using a Pandas dataframe.

The following screenshot shows how to perform the aggregation in a dataframe. In the lower half, the data is assigned to a new dataframe.

The following code demonstrates how to use the Pandas ExcelWriter to make a multi-page Excel workbook. Each dataframe uses to_excel function and sets the sheet_name.

The following shows the resulting multi-page workbook.

Pandas to Excel – MultiPage Workbook

Leave a Reply

Your email address will not be published. Required fields are marked *