按照下面的示例,我正在将数据帧输出到电子邮件正文。是否可以在列标签行上方添加标题行或标题行?最好是一行,即一个单元格,即数据帧的长度,其中包含一个字符串?
import pandas as pd
import numpy as np
from IPython.core.display import display, HTML
dates = pd.date_range('20130101',periods=3)
df = pd.DataFrame(np.random.randn(3,4),index=dates,columns=list('ABCD'))
styles = [{'props':[("font-family", "Calibri")]}, {
'selector': 'th',
'props': [
('background-color', 'yellow')]}]
s = df.style.set_table_styles(styles)
html = s.hide_index().render()
with open("html_c.html","w") as fp:
fp.write(html)
#to display in a jupyter notebook
display(HTML(html))
当前输出:
所需的输出:
答案 0 :(得分:1)
也许是这样吗?
import pandas as pd
import numpy as np
from IPython.core.display import display, HTML
dates = pd.date_range('20130101',periods=3)
### >>> begin
columns = list("ABCD")
columns = list(zip(['HEADER'] * 4, columns))
#[('HEADER', 'A'), ('HEADER', 'B'), ('HEADER', 'C'), ('HEADER', 'D')]
columns = pd.MultiIndex.from_tuples(columns, names=['first', 'second'])
### >>> end
df = pd.DataFrame(np.random.randn(3,4),index=dates,columns=columns) # <--- also here
styles = [{'props':[("font-family", "Calibri")]}, {
'selector': 'th',
'props': [
('background-color', 'yellow'),
('text-align','center')] # for alignment
}]
s = df.style.set_table_styles(styles)
html = s.hide_index().render()
with open("html_c.html","w") as fp:
fp.write(html)
#to display in a jupyter notebook
display(HTML(html))