How pandas to_excel works in Python? Best example

How pandas to_excel works in Python? Best example
“`html

Working with data in Python often means dealing with spreadsheets, and that’s where the pandas.to_excel() function becomes incredibly useful. Whether you’re exporting data to share with colleagues or storing results from your data processing pipeline, this function makes saving a pandas.DataFrame to an Excel file effortless.

Understanding pandas.to_excel()

The to_excel() method allows us to export a DataFrame to an Excel file. The basic syntax is:

import pandas as pd

# Sample DataFrame
df = pd.DataFrame({
    'Name': ['Alice', 'Bob', 'Charlie'],
    'Age': [25, 30, 35],
    'Score': [85, 90, 88]
})

# Save DataFrame to Excel
df.to_excel("output.xlsx", index=False)

This saves the DataFrame to an Excel file named output.xlsx in the current working directory. The index=False parameter ensures that the index column is not saved.

Key Parameters of to_excel()

The function comes with several parameters that provide greater control over the output:

  • excel_writer: The file path or an Excel writer object.
  • sheet_name: The name of the Excel sheet where the data will be written (default: “Sheet1”).
  • na_rep: A string representation for missing values (e.g., na_rep="N/A").
  • float_format: Format for floating-point numbers (e.g., float_format="%.2f").
  • columns: A list of column names to be written to the file.
  • header: Whether to write column labels (default: True).
  • index: Whether to include the DataFrame index as a column.

Writing to Multiple Sheets in Excel

Sometimes, we need to export multiple DataFrames into a single Excel file with different sheets. Here’s how:

with pd.ExcelWriter("multi_sheet.xlsx", engine="xlsxwriter") as writer:
    df.to_excel(writer, sheet_name="Sheet1", index=False)
    
    # Another DataFrame example
    df2 = pd.DataFrame({
        "Product": ["Book", "Pen", "Notebook"],
        "Quantity": [10, 50, 30]
    })
    
    df2.to_excel(writer, sheet_name="Sheet2", index=False)

This will create an Excel file named multi_sheet.xlsx with two sheets: Sheet1 and Sheet2.

Customizing Column Widths and Styles

Using the XlsxWriter engine, we can enhance the appearance of our Excel output:

with pd.ExcelWriter("styled.xlsx", engine="xlsxwriter") as writer:
    df.to_excel(writer, sheet_name="Sheet1", index=False)
    
    workbook = writer.book
    worksheet = writer.sheets["Sheet1"]
    
    # Set column width
    worksheet.set_column("A:C", 20)
    
    # Apply formatting
    format1 = workbook.add_format({"num_format": "0.00"})
    worksheet.set_column("C:C", None, format1)

Here, we adjusted the width of the columns and applied a numeric format to column C.

Handling Large Excel Files Efficiently

When dealing with large datasets, optimizing memory usage is important. Consider these tips:

  • Use openpyxl instead of xlsxwriter when editing existing files.
  • Set index=False to reduce unnecessary data.
  • Write data in chunks if required.

Comparison: pandas.to_excel() vs. to_csv()

Feature to_excel() to_csv()
File Format Excel (.xlsx, .xls) CSV (plain text)
Supports Formatting Yes No
Multiple Sheets Yes No
File Size Larger Smaller

Use Excel files when formatting is important and multiple sheets are needed. For simple data exchange, CSV files are often more efficient.

Conclusion

Understanding how pandas.to_excel() works in Python opens up powerful options for exporting and managing your data. From writing simple sheets to formatting custom reports, this function is a must-have in any data analyst’s toolkit. Experiment with different parameters to find the best way to generate your reports efficiently.

“` Other interesting article: How pandas to_csv works in Python? Best example