1

我正在使用 df.style.applymap 来呈现熊猫数据框。我选择了样式而不是 to_html,因为我需要根据某些列值应用 css。挑战是我想从显示中排除其中一列。因此,使用以下代码:

html = df.style.applymap(color_cell, subset=['columnName']).render()
with open('output.html', 'w') as f:
    f.write(html)

有没有办法可以排除/隐藏我需要为其他列设置样式的列?

4

2 回答 2

8

如果您需要隐藏在函数 color_cell 中的列,您可以使用 styler 的 hide_columns 功能。

df.style.hide_columns([hide_cols_list]).applymap(color_cell, subset=['columnname' + hide_cols_list]).render()
于 2019-02-28T12:01:16.593 回答
0

您可以在呈现 HTML 时删除要隐藏的列:

cols_2_hide = ['col1','col2']
df.drop(cols_2_hide, axis=1).style.applymap(color_cell, subset=['columnName']).render()
于 2018-03-12T16:17:20.830 回答