0

I am using the Pandas DataFrame styler and want to make every other column (based on the first level of a multi index rather than hard code) have a gray background. I know that with CSS you can use the column combinator to say something like

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

but it doesn't seem to be compatible with pandas styling.

In my DataFrame I have level 0 of a multi index as months and then level 1 as some other columns. The end goal is to have every other month be highlighted in some way.

Any help would be greatly appreciated.

nerrez
  • 15
  • 3

1 Answers1

0

Pandas does not create column groups in HTML, and the technology is experimental in HTML anyway, so this won't work.

You are better off doing something like:

    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]
    })

enter image description here

You will have to be a little more creative to structure the dict-comprehension according to your multiindex columns.

Attack68
  • 4,437
  • 1
  • 20
  • 40