I'm trying to display several pandas dataframes horizontally in a Jupyter notebook, but am running into a situation where the styler's HTML is not formatted correctly.
I have the following code snippet:
import numpy as np
import pandas as pd
from IPython.display import display, HTML
def display_multi_table(table_list, with_display):
if with_display:
for t in table_list: display(t)
return HTML(
'<table><tr style="background-color:white;">' +
''.join(['<td style="height:300px;width:300px">' + table.render() + '</td>' for table in table_list]) +
'</tr></table>'
)
d = dict([(f"column_{i}", np.random.rand(5)) for i in range(3)])
tables = [pd.DataFrame(d).style.set_caption("Title") for i in range(3)]
The following call:
display_multi_table(tables, False)
The following call:
display_multi_table(tables, True)
My goal: Achieve the output at the bottom of the second screenshot without having to call display
.
Things I've tried/checked:
- If I don't style the pandas dataframe, and use simply call
.to_html()
on the dataframe instead of calling.render()
on the styler, this issue does not exist. However, I need this to work on a style dataframe. - I've confirmed that the value returned by
table.render()
before and after callingdisplay
is indeed different.