我试图用一个粗体栏返回 df.to_html()。我只试过
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()]
但是好像不行。有人知道实用的解决方案吗?
最佳答案
您可以使用 df.style.set_properties
然后 .render()
这将为 .to_html()
的普通表输出添加前缀与适当的 style
元素。 (请注意,这不会将您的文本元素物理包装在 <b>
或 <strong>
或您想要的任何标签中,而是纯粹为这些单元格提供样式 - 根据用例,这可能是您想要的,也可能不是您想要的)
html = df.style.set_properties(
subset=['important_column'],
**{'font-weight': 'bold'}
).render()
(jupyter 笔记本中显示的示例)
关于python - pandas DataFrame to_html 中的粗体列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52110530/