Is it possible to export output of Jupyter notebook to Excel?

Viewed 4601

I imported an excel file, performed a Group By on a certain column in a dataframe, now I want to export that output (sort of pivot) back in the excel workbook in another sheet, is it possible to do that?

3 Answers

By import you mean you can write back to excel, and of course you can by using the ExcelWriter in pandas.
You can do it like so:

import pandas as pd

# Read and manipulate you DataFrame
       ...Lines of Codes...
# Create a Pandas Excel writer using XlsxWriter as the engine.
writer = pd.ExcelWriter('your_file_name.xlsx', engine='xlsxwriter')

# Write dataframe to new sheet
df.to_excel(writer, sheet_name='AnotherSheet')

# Close the Pandas Excel writer and output the Excel file.
writer.save()

Since this can be done in python, it can surely be done in jupyter-notebook

Short method is if your output is in DataFrame, you can use this function to export output as excel. suppose output is in variable name out. Then do that:

out.to_excel(r'Path\File Name.xlsx', sheet_name='Your sheet name', index = False)
Related