Formatting Excel with Pandas: A Guide to Colorful Data Analysis

In the realm of data analysis, Python's pandas library and Microsoft Excel are two powerful tools that often go hand in hand. While pandas excels at data manipulation and analysis, Excel provides an intuitive interface for data visualization and sharing. This guide will walk you through the process of formatting your pandas DataFrame and exporting it to Excel with colorful, eye-catching styles.

Understanding Pandas and Excel Formatting
Pandas DataFrames are two-dimensional, size-mutable, and heterogeneous tabular data structures with labeled axes (rows and columns). When it comes to formatting, pandas offers several styling options, but it's important to note that these styles won't be preserved when you export your DataFrame to Excel. To achieve colorful, formatted Excel files, we'll use the `openpyxl` engine with pandas' `to_excel` function.

Installing Required Libraries
Before we dive into the formatting process, ensure you have the `openpyxl` library installed. If not, you can install it using pip:

pip install openpyxl
Formatting with Pandas Styling
Pandas styling allows you to apply formatting to your DataFrame in a way that's easy to read and understand. Let's create a simple DataFrame and apply some styles:

import pandas as pd
import numpy as np
# Create a simple dataframe
df = pd.DataFrame({
'A': [1, 2, 3, 4],
'B': [10, 20, 30, 40],
'C': [100, 200, 300, 400]
})
# Apply styles
styled_df = df.style \
.applymap(lambda x: 'color: red' if x > 100 else '') \
.applymap(lambda x: 'background-color: yellow' if x > 20 else '') \
.highlight_max(color='lightgreen') \
.set_properties(subset=['A'], **{'background-color': 'lightblue'})
# Display the styled dataframe
styled_df
Exporting to Excel with Formatting
Now that we have our DataFrame styled, let's export it to Excel with the same formatting. We'll use the `openpyxl` engine and apply the styles using `ExcelWriter`:

with pd.ExcelWriter('formatted_data.xlsx', engine='openpyxl') as writer:
df.to_excel(writer, sheet_name='Sheet1', index=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']
worksheet.add_table(0, 0, len(df), len(df.columns), {'style': 'TableStyleLight1'})
# Open the formatted Excel file
formatted_data = pd.read_excel('formatted_data.xlsx')
formatted_data
Customizing Excel Styles




















The `TableStyleLight1` style used in the previous example is just one of many built-in styles you can apply to your Excel table. To create a more customized look, you can modify the styles using the `openpyxl` library:
from openpyxl import load_workbook
# Load the workbook
wb = load_workbook('formatted_data.xlsx')
# Get the worksheet
ws = wb['Sheet1']
# Access the table style
table_style = ws.table.style
# Modify the style (e.g., change the font color to blue)
table_style.font.color = 'blue'
# Save the workbook
wb.save('formatted_data_custom.xlsx')
Conclusion
In this guide, we've explored how to format pandas DataFrames and export them to Excel with colorful, engaging styles. By combining the power of pandas for data manipulation and the visual appeal of Excel, you can create data analysis reports that are both informative and easy on the eyes. Happy data crunching!