7

我正在尝试使用通过在电子邮件中呈现样式器生成的字符串。似乎很难让这个忽略数据帧索引。

table_styles = [dict(selector="tbody tr th", props=[("display", "none")]),
st=df.style.set_table_styles(table_styles)
st.render()

我已经能够让它与 display none CSS 设置一起工作,但它在基于 CSS 支持级别的不同设备上的工作方式不同。
没有办法让索引有效负载消失吗?

4

5 回答 5

1

我想我的解决方案与您的解决方案相同,并且面临与您相同的问题(不同设备上的显示不同)。我只是在这里写下部分解决方案,以帮助正在寻找方法的人。

如果你这样做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)
于 2016-11-07T20:45:41.297 回答
0

styler.hide_index() 方法已被弃用,取而代之的是 `Styler.hide(axis='index') (版本 1.4.0)

对我 df.style.hide(axis='index')有用。

于 2022-02-02T07:48:48.533 回答
0

我不确定我们是否有同样的问题,但我的问题是使用.style.render(). 我尝试使用 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 文件中。

于 2018-08-08T15:39:20.170 回答
0

由于版本 0.23.0 pandas 带有styler.hide_index()函数,将其与要应用的其他方法链接起来,因为返回的对象仍将是 styler 对象

如果 hide_index() 不可用,请尝试更新您的 pandas 库

于 2019-11-13T01:57:54.833 回答
0

根据熊猫样式文档部分:隐藏索引或列足以使用.hide_index(). 在您的情况下,它将是:

table_styles = [dict(selector="tbody tr th", props=[("display", "none")]),
st=df.style.set_table_styles(table_styles).hide_index()
st.render()
于 2018-08-30T09:22:56.013 回答