Python 熊猫造型师。如何从呈现的HTML中忽略索引列
我试图使用在电子邮件消息中呈现样式器时生成的字符串。忽略数据帧索引似乎很难做到这一点Python 熊猫造型师。如何从呈现的HTML中忽略索引列,python,css,pandas,Python,Css,Pandas,我试图使用在电子邮件消息中呈现样式器时生成的字符串。忽略数据帧索引似乎很难做到这一点 table_styles = [dict(selector="tbody tr th", props=[("display", "none")]), st=df.style.set_table_styles(table_styles) st.render() 我已经能够使用display none CSS设置,但它在不同的设备上的工作方式不同,具体取决于CSS支持级别。 难道没有办法让索引有效负载消失吗?我想
table_styles = [dict(selector="tbody tr th", props=[("display", "none")]),
st=df.style.set_table_styles(table_styles)
st.render()
我已经能够使用display none CSS设置,但它在不同的设备上的工作方式不同,具体取决于CSS支持级别。难道没有办法让索引有效负载消失吗?我想我的解决方案与您的相同,我面临的问题与您相同(不同设备上的显示不同)。我只是在这里写下部分解决方案,以帮助正在寻找解决方法的人 如果执行
html.render().split('\n')
,您将能够获得与第一列和索引相关的类结构(如果您已经使用了resent_index)
然后,可以使用display的CSS属性定义样式以除去这些列
# define the style related to first column and index here
# first-element : when index =True,
# second element: default index of the table generated by the Style
styles = [
dict(selector = ".col0", props = [('display', 'none')]),
dict(selector = "th:first-child", props = [('display', 'none')])
]
# set the table styles here
table.set_table_styles(styles)
我不确定我们是否遇到了相同的问题,但我的问题是使用
.style.render()
将df保存到HTML文件中。我尝试过使用CSS样式,但它不起作用。我使用的解决方案非常简单,我没有考虑它,我基本上运行.hide_index()
,如下所示:
html = ts_volume_html.style.format({'total_ops': "{:.2f}", 'read_ops': '{:.2f}','read_data(bps)':'{:.2f}',
'read_latency(us)': "{:.2f}", 'write_ops': '{:.2f}','write_data(bps)':'{:.2f}',
'write_latency(us)': "{:.2f}", 'other_ops': '{:.2f}','other_latency(us)':'{:.2f}', \
.set_table_attributes('border="1" class="dataframe table table-hover table-bordered"')\
.set_precision(2).set_properties(**{'font-size': '10pt', 'font-family': 'Courier New'})\
.hide_index().background_gradient(cmap=cm).set_table_styles([{'selector': 'th', 'props': [('font-size', '10pt')]}]).render()
然后我将“html”str保存到一个html文件中。根据熊猫造型文档部分:使用
.hide_index()
就足够了。在您的情况下,它将是:
table_styles = [dict(selector="tbody tr th", props=[("display", "none")]),
st=df.style.set_table_styles(table_styles).hide_index()
st.render()
因为版本0.23.0附带了一个函数,所以将其与要应用的其他方法链接起来,因为返回的对象仍然是样式器对象
如果hide_index()不可用,请尝试更新您的pandas库Hi,我遇到了类似问题。你解决这件事运气好吗?