pythonpandaspandas-styles

Style Pandas Columns Separately


I want to apply separate background gradient to different columns in my pandas dataframe

a b c
-----
1 2 3
4 5 6
9 2 3

I want one background gradient for column a and a different background gradient to another column as such.

How do I do that?


Solution

  • You can apply background_gradient to each individual column and compose multiple styles in a single call:

    df.style\
        .background_gradient(cmap="cool", subset=['a'])\
        .background_gradient(cmap="bone", subset=['b'])\
        .background_gradient(cmap="winter", subset=['c'])
    

    enter image description here