I'm trying to highlight specific cells for each column with different condition which their value matches the condition for each row.
Below image is what I want to achieve: The table I attempt to achieve
I searched google and stackoverflow but none of these can meet my requirement. Can anyone who's familiar with Pandas Style could assist?
Below are the codes I tried and failed:
Ex1
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
def highlight(s):
return ['background-color: yellow' if (v>2) else 'background-color: white' for v in s]
df.style.apply(highlight, axis=0)
Ex2
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
Column_limit = (df['A'] > 6) | (df['B'] > 2) | (df['C'] < 3)
df[Column_limit].style.applymap(lambda x: 'background-color: yellow', subset=pd.IndexSlice[:, ['A', 'C']])
Ex3
import pandas as pd
df = pd.DataFrame([[10,3,1], [3,7,2], [2,4,4]], columns=list("ABC"))
subsets = pd.IndexSlice[:, 'A']
df.style.applymap(lambda x: 'background-color: yellow', subset = subsets)