Copying the ExcelFormat styling of a DataFrame

32 views Asked by At

When using the to_excel function of pandas DataFrames, the formatter ExcelFormatter styles the cells in a nice way (for example, it merges repeated multiindex columns). Is it possible to copy this style in the display function of IPython.display so that Jupyter notebooks DataFrames are displayed like this too?

Thanks

1

There are 1 answers

0
Daqs On

You can use HTML and CSS, however.

from IPython.display import display, HTML

styled_html = df.to_html(classes='my_table_style')

# Define your CSS
css = """
<style type='text/css'>
.my_table_style {
    font-size: 12px;
    border-collapse: collapse;
    ...
}
</style>
"""

display(HTML(css + styled_html))