I am trying to return df.to_html() with one bold column. I have only tried
df = pd.DataFrame({'important_column': [1,2,3,4],
'dummy_column': [5,6,7,8]})
def some_function()
df.apply(lambda x: '<b>' + str(df['important_column']) + '</b>', axis=1)
return [df.to_html()]
But it doesn't seem to work. Does any one know a practical solution?
You can use a df.style.set_properties
and then .render()
which'll prefix the normal table output from .to_html()
with an appropriate style
element. (note this does not physically wrap your text elements inside a <b>
or <strong>
or whatever tags you wish but purely provides styling for those cells - which may or may not be what you want depending on the use case)
html = df.style.set_properties(
subset=['important_column'],
**{'font-weight': 'bold'}
).render()
(example shown in jupyter notebook)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With