Home > Net >  styles.background_gradient but invers
styles.background_gradient but invers

Time:12-30

I´m setting a table with pandas styler, but I wonder if is it possible to sort the background_gradient in decreasing way.

Just the contrary of this:

.background_gradient(subset=pd.IndexSlice[:6,['Año 2022']], axis=0)\

Normal background_gradient way

CodePudding user response:

Asusume you have the following dataframe:

   Year  stations  Views
0  2015         1    100
1  2015         2     85
2  2015         3     75
3  2015         4     25
4  2015         5     10
5  2015         6    100

You can define a gradient map and then define that it is the inverse of the dataframe:

So, without:

gmap = np.array([[1,2,3], [2,3,4], [3,4,5]])
df.style.background_gradient(subset=['Views'],axis=None,cmap="Blues", gmap=-df)

will give

enter image description here

while

gmap = np.array([[1,2,3], [2,3,4], [3,4,5]])
df.style.background_gradient(subset=['Views'],axis=None,cmap="Blues", gmap=df)

will return:

enter image description here

  • Related