有没有办法在 Pandas DataFrame 样式中使用 CSS 列组合器?

Is there a way to use CSS column combinator in Pandas DataFrame Styling?

我正在使用 Pandas DataFrame 样式器并希望使每隔一列(基于多索引的第一级而不是硬代码)具有灰色背景。我知道使用 CSS 你可以使用列组合器来表示

Th:nth-child(2n) || td

但它似乎与 pandas 样式不兼容。

在我的 DataFrame 中,我将多索引的 0 级作为月份,然后将 1 级作为其他一些列。最终目标是每隔一个月以某种方式突出显示。

如有任何帮助,我们将不胜感激。

Pandas 不会在 HTML 中创建列组,而且该技术在 HTML 中仍然是实验性的,所以这行不通。

你最好做这样的事情:

    df = pd.DataFrame(np.arange(20).reshape(2,10))
    style = [{"selector": "td", "props": "background-color: grey; color: white;"}]
    df.style.set_table_styles({
        col: style for col in df.columns[::2]
    })

根据您的多索引列构建字典理解,您需要更有创意。