python 为html页面增加背景_Pandas dataframe.to_html()-将背景色添加到头部

您可以尝试以下两种方式:import pandas as pd

import numpy as np

Set up a DataFrame

np.random.seed(24)

df = pd.DataFrame({‘A’: np.linspace(1, 10, 10)})

axis=1)

df.iloc[0, 2] = np.nan

df_html_output = df.style.set_table_styles(

[{‘selector’: ‘thead th’,

‘props’: [(‘background-color’, ‘red’)]},

{‘selector’: ‘thead th:first-child’,

‘props’: [(‘display’,’none’)]},

{‘selector’: ‘tbody th:first-child’,

‘props’: [(‘display’,’none’)]}]

).render()

html.append(df_html_output)

body = ‘\r\n\n
‘.join(‘%s’%item for item in html)

msg.attach(MIMEText(body, ‘html’))

或者用^{}:df_html_output = df.to_html(na_rep = “”, index = False).replace(‘

‘,”)

html.append(df_html_output)

body = ‘\r\n\n
‘.join(‘%s’%item for item in html)

msg.attach(MIMEText(body, ‘html’))

第二个选项提供了在导出期间删除索引列(to_html)的选项,而不必做太多的HTML调整;因此它可能更适合您的需要。

我希望这证明是有用的。

Original: https://blog.csdn.net/weixin_32408723/article/details/113964876
Author: iaintdenny
Title: python 为html页面增加背景_Pandas dataframe.to_html()-将背景色添加到头部

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/678986/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球