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.

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.