我很难用Pandas的“to-html”方法应用“classes”参数来设置数据帧的样式。
“类:str或list或tuple,默认为None 应用于生成的html表的CSS类“ 发件人:https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_html.html
我能够呈现这样的样式化数据帧(例如):
df = pd.DataFrame([[1, 2], [1, 3], [4, 6]], columns=['A', 'B'])
myhtml = df.style.set_properties(**{'font-size': '11pt', 'font-family': 'Calibri','border-collapse': 'collapse','border': '1px solid black'}).render()
with open('myhtml.html','w') as f:
f.write(myhtml)
如何将“类”与“to-html”一起用于数据帧的html输出样式,如下所示:
df.to_html('myhtml.html',classes=<something here>)
我是这样做的
为css代码创建一个文本文件并在这里写下css代码,比如css_style.txt 现在将这个txt文件作为python文件中的字符串读取
with open('css_style.txt', 'r') as myfile: style = myfile.read()
现在是HTML代码
在我的例子中,css_style.txt文件是
Pandas'
to_html
只输出包含HTML表标记的大字符串。classes参数是一个方便的处理程序,它为<table>
aclass属性提供便利,该属性将在先前创建的CSS文档中引用。因此,将to_html
合并到引用外部CSS的更广泛的HTML文档构建中。有趣的是,
to_html
添加了两个类<table class="dataframe mystyle">
,它们可以在CSS中单独引用,.dataframe {...} .mystyle{...}
,也可以一起引用。下面用随机数据演示。数据
CSS(另存为df_style.CSS)
熊猫
输出
HTML(引用df_style.css,假定在同一目录中;请参阅表中的类参数)
相关问题 更多 >
编程相关推荐