nik
nik

Reputation: 1784

background_gradient by dataframe not by column or row

This is very close to what I need:

index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
cols = ['A', 'B', 'C', 'D']
df = pd.DataFrame(np.random.randn(5, 4), index=index, columns=cols)
df.style.background_gradient(cmap='Reds')

The only issue is that the colour shading is based on ranking of values by column. I would like to apply it to all numbers in the frame. So the largest x% in the dataframe should get the darkest red. Like this, if you look at the frame as a whole, numbers share the same colour that are very different in terms of magnitude.

Upvotes: 0

Views: 711

Answers (1)

Cameron Riddell
Cameron Riddell

Reputation: 13407

I believe you're looking for the axis=None argument:

index= ['aaa', 'bbb', 'ccc', 'ddd', 'eee']
cols = ['A', 'B', 'C', 'D']
df = pd.DataFrame(np.random.randn(5, 4), index=index, columns=cols)
df["B"] *= 10


df.style.background_gradient(cmap='Reds', axis=None)

enter image description here

Upvotes: 1

Related Questions