I am currently working with a Pandas DataFrame and I am looking for a way to display it on an HTML page with minimal empty space. Additionally, I am utilizing Bootstrap 4.
To format all elements of a column, I can use the to_html
method along with table styles:
html = df.to_html(
formatters={'COL_NAME': lambda x: '<b>' + x + '</b>'},
classes='table table-striped df_data',
escape=False
)
Furthermore, I can directly add HTML elements by replacing the values like so:
html = html.replace('<th>', '<th><div><span>')
html = html.replace('</th>', '</span></div></th>')
- Is there a tidy method to incorporate these HTML elements into the headers?
I am following these steps to display rotated headers and optimize screen usage as shown in the image. I sourced the CSS styles from this page
.df_data thead th { height: 140px; white-space: nowrap; } .df_data thead th > div { transform: translate(25px, 51px) rotate(315deg); width: 30px; } .df_data thead th > div > span { border-bottom: 1px solid #ccc; padding: 5px 10px; }
However, the outcome is not as expected. The space is not optimized and the headers are misaligned. What adjustments should I make to maximize column space? I am willing to forego Bootstrap styles.
Is there a more direct approach to achieve this?
Any suggestions or recommendations would be greatly appreciated
Update 2019-26-02
The solution provided by @joshmoto seems promising, but I need to adjust column widths as I cannot predict the cell contents in advance (maximum of 10 characters).