gpt4 book ai didi

python - pandas DataFrame to_html 中的粗体列

转载 作者:行者123 更新时间:2023-11-27 23:01:28 28 4
gpt4 key购买 nike

我试图用一个粗体栏返回 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 笔记本中显示的示例)

enter image description here

关于python - pandas DataFrame to_html 中的粗体列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52110530/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com