Pandas background_gradient only for values above a given threshold

Viewed 47

I have the following code:

# using apply
import pandas as pd
df = pd.DataFrame({"Values A":[1,2,3,4,5], "Values B":[0,11,12,13,15]})

df.style.background_gradient(subset = "Values B")

However this code changes the color of all cells in column Values B. I would like to change the color only for cells which values for example are bigger than 11, so 12,13, 15. How can I do that? Adding vmin = 11 didnt solve my problem beacuse the color of cells below 11 is still changed.

enter image description here

1 Answers

Just subset rows, as well as you do columns.

df.style.background_gradient(subset=(df["Values B"]>11, "Values B"))

Use greater than or equal to 11 if necessary.

enter image description here

Related