Explore Courses Blog Tutorials Interview Questions
0 votes
in Data Science by (50.2k points)

I'm having trouble applying the "classes" argument with the Pandas "to_html" method to style a DataFrame.

"classes: str or list or tuple, default None CSS class(es) to apply to the resulting Html table" from

I am able to render a styled DataFrame like this (for example):

df = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=['A', 'B'])

myhtml =**{'font-size': '11pt', 'font-family': 'Calibri','border-collapse': 'collapse','border': '1px solid black'}).render()

with open('myhtml.html','w') as f:


How can I style html output from a DataFrame using "classes" with "to_html" like this:

df.to_html('myhtml.html',classes=<something here>)

1 Answer

0 votes
by (108k points)

Pandas' to_html simply provides the output of a large string containing HTML table markup. The class argument is a convenience handler to give the <table> a class characteristic that will be referenced in a previously created CSS document that styles it. Hence, incorporate to_html into a wider HTML document build that references an external CSS.

Interestingly, to_html adds dual classes <table class="dataframe mystyle"> which can be referenced in CSS individually, .dataframe {...} .mystyle{...}, or together .dataframe.mystyle {...}.

Browse Categories