如何修改Pandas DataFrame表头样式及HTML表格表头字号?
Hey there! Let's tackle your two questions about styling Pandas DataFrame headers—both when working within Pandas and when generating HTML tables where you're stuck on adjusting the header font size.
If you're working with Pandas' built-in Styler object (great for notebooks or exporting to Excel/HTML), you can use set_table_styles() to target header cells and tweak their appearance, including font size.
Here's a concrete example:
import pandas as pd # Create a sample DataFrame df = pd.DataFrame({ 'Sales': [1200, 1500, 900], 'Expenses': [800, 1000, 700], 'Profit': [400, 500, 200] }) # Customize the header style styled_df = df.style.set_table_styles([ { 'selector': 'th', # Targets all header cells 'props': [ ('font-size', '16px'), # Set your desired font size here ('background-color', '#f2f2f2'), # Optional: add a background color ('font-weight', '700'), # Optional: make text bold ('text-align', 'center') # Optional: center-align text ] } ]) # Display in Jupyter Notebook styled_df # Or export to Excel (requires openpyxl) # styled_df.to_excel('styled_finance.xlsx', engine='openpyxl')
The selector: 'th' targets all table header cells, and the props list lets you add any CSS properties you want—just use the CSS property name (like font-size) and your desired value.
If you're generating HTML tables (either via df.to_html() or Styler.to_html()), you have a couple of straightforward ways to fix the header font size:
Option 1: Use Styler to Generate Styled HTML
Build on the styled DataFrame we created above, then export it directly to HTML. This preserves all your header styles automatically:
# Export the styled DataFrame to HTML html_output = styled_df.to_html() # Save to a file with open('styled_table.html', 'w') as f: f.write(html_output)
Open the HTML file, and your headers will have the font size (and other styles) you defined.
Option 2: Add Custom CSS to Plain HTML Tables
If you're using plain df.to_html() without the Styler, you can inject custom CSS to target the headers. This is useful if you want to modify existing HTML output:
# Generate a basic HTML table basic_html = df.to_html() # Wrap it in custom CSS to style headers custom_html = f""" <style> /* Target all header cells in the table */ th {{ font-size: 18px !important; /* Use !important to override default styles */ background-color: #e6e6e6; color: #2d2d2d; }} </style> {basic_html} """ # Save the styled HTML with open('custom_header_table.html', 'w') as f: f.write(custom_html)
The !important flag ensures your font size overrides any default styles Pandas adds to the HTML table.
Option 3: Use Custom Table Classes (For More Control)
If you have multiple tables and want to style them differently, add a custom class to your table and target that class in CSS:
# Generate HTML with a custom table class class_html = df.to_html(classes='sales-table') # Style only headers in tables with the 'sales-table' class custom_class_html = f""" <style> .sales-table th {{ font-size: 17px; text-transform: uppercase; /* Optional: make headers uppercase */ border-bottom: 2px solid #333; }} </style> {class_html} """
This keeps your styles scoped to specific tables, avoiding conflicts with other elements on a page.
内容的提问来源于stack exchange,提问作者Han

