Exporting a DataFrame as an Excel file#

Pandas can do this natively with the openpyxl library, a Python library for reading/writing Excel 2010 xlsx/xlsm/xltx/xltm files.

Importing libraries and packages#

1# Mathematical operations and data manipulation
2import pandas as pd

Set paths#

1# Path to datasets directory
2data_path = "./datasets"
3# Path to assets directory (for saving results to)
4assets_path = "./assets"

Loading dataset#

1dataset_1 = pd.read_csv(f"{data_path}/example_1.csv")
2dataset_1
Column 1 Column 2 Column 3 Column 4
0 2 1500 Good 300000
1 3 1300 Fair 240000
2 3 1900 Very good 450000
3 3 1850 Bad 280000
4 2 1640 Good 310000

Writing dataset#

1writer = pd.ExcelWriter(f"{assets_path}/example_1.xlsx")
2dataset_1.to_excel(writer, "Sheet1")
3writer.save()
4writer
<pandas.io.excel._openpyxl.OpenpyxlWriter at 0x7fa7eca4f5e0>