0

我正在将 pandas 数据框导出为 html 表,并且一直在玩弄最终表中标题列的样式。这是我生成的示例数据框:

x = np.arange(0,100,5)
y = np.arange(0,20,1)
example_df = pd.DataFrame(x,y).head(10).reset_index()
example_df.columns = ['X Column', 'Y Column']

我已经能够使用以下代码编辑标题文本颜色:

example_df = example_df.to_html(index=False).replace('<th>','<th style = "color", "White">')

我还能够使用以下代码单独更改背景颜色:

example_df = example_df.to_html(index=False).replace('<th>','<th style = "background-color", "RoyalBlue">')

虽然我已经能够分别更改列标题文本颜色和背景颜色,但我无法同时更改两者。有没有更好的方法来实现这一点,以便我能够同时更改文本颜色和背景颜色,而不是在仅更改一个或另一个之间进行选择?谢谢!

4

1 回答 1

2

这将起作用 -

> example_df.to_html(index=False).replace('<th>','<th style =
> "background-color: royalblue; color: white">')
于 2021-08-19T11:57:44.090 回答