次の2つの方法でこれをやって試すことができます:
set_table_styles
でpandas.DataFrame.style
から:
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)})
df = pd.concat([df, pd.DataFrame(np.random.randn(10, 4), columns=list('BCDE'))],
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<br>'.join('%s'%item for item in html)
msg.attach(MIMEText(body, 'html'))
または.to_html
と:
df_html_output = df.to_html(na_rep = "", index = False).replace('<th>','<th style = "background-color: red">')
html.append(df_html_output)
body = '\r\n\n<br>'.join('%s'%item for item in html)
msg.attach(MIMEText(body, 'html'))
は、第1には、あまりにも多くのHTML
微調整を行うことなく、輸出(to_html
)中にインデックス列を削除するオプションを提供します。あなたのニーズにより適しているかもしれません。
私はこれが有用であることを望みます。
['pandas.DataFrame.style'](http://pandas.pydata.org/pandas-docs/version/0.17.1/generated/pandas.core.style.Styler.html)を参照してください。 'set_table_styles'があなたの' HTML'属性を設定するために使用でき、 '.render()'メソッドを使ってHTMLスクリプトを取得することができます。 – Abdou
@Abdou 'set_table_styles'に適切な属性を与える方法の例を挙げてください。私は比較的新しいHTMLとCSSを使っています – Zedak