【发布时间】:2022-11-12 14:43:06
【问题描述】:
我正在尝试使用 pandas 1.3.5 隐藏 pandas Styler 的索引级别。IE。在 pandas 1.4 中复制 hide(axis="index", level="level_name") 的行为。
这是我正在尝试的一个最小示例:
def multi_highlighter(row, range_colors):
def find_color(value):
for color, threshold in range_colors.items():
if value < threshold:
return color
return "white"
return [f"background-color: {find_color(v)}" for v in row]
range_colors = {"red": 18, "orange": 100}
data = pd.DataFrame({
"Ex Date": ['2022-06-20', '2022-06-20', '2022-06-20', '2022-06-20', '2022-06-20', '2022-06-20', '2022-07-30', '2022-07-30', '2022-07-30'],
"Portfolio": ['UUU-SSS', 'UUU-SSS', 'UUU-SSS', 'RRR-DDD', 'RRR-DDD', 'RRR-DDD', 'KKK-VVV', 'KKK-VVV', 'KKK-VVV'],
"Position": [120, 90, 110, 113, 111, 92, 104, 110, 110],
"Strike": [18, 18, 19, 19, 20, 20, 15, 18, 19],
})
(
data
.reset_index()
.set_index(["Ex Date", "Portfolio", "index"])
.style
.apply(multi_highlighter, range_colors=range_colors, axis=1)
)
添加一些边框,它会生成下表:
现在要隐藏“索引”索引级别,我尝试通过从 answer 添加 CSS 样式来实现这一点,如下所示:
.set_table_styles([{'selector': 'tr th:nth-child(3), tr td:nth-child(3)', 'props': [
('display', 'None')]}], overwrite=False)
但它给了我这个结果:
【问题讨论】:
标签: html css python-3.x pandas pandas-styles