[英]how to maintain the look and feel of a table after adding a color
在我的 jupyter 实验室中,我总是添加这个单元格以使桌面看起来整洁:
%%HTML
<style type="text/css">
table.dataframe td, table.dataframe th {
border: 1px solid lightgray;
font-size: 12px;
font-family: Verdana;
}
</style>
df = pd.DataFrame({"A": [*"123123123"], "B": [*"abcdefghi"]})
原始结果:
现在我想在值为 h 时突出显示 B 行:
def style_specific_cell(x):
c1 = 'background-color: red'
c2 = ''
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
m = x['B'] == 'h' #hard coded just for the example
df1['B'] = np.where(m, c1, c2)
return df1
并在运行样式时:
df.style.apply(style_specific_cell, axis=None)
我得到:
结果很好,但外观和感觉不佳。 我希望按照 HTML 的外观维护原始结果表。 我怎样才能做到这一点?
显然,可以添加 set_table_styles 来定义表格的最终结果。 所以一个例子是:
df.style.apply(highlight, axis=None).set_table_styles(
[{"selector": "", "props": [("border", "1px solid")]},
{"selector": "tbody td", "props": [("border", "1px solid")]},
{"selector": "th", "props": [("border", "1px solid")]}])
感谢@Jean123 在此处的回答。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.