I'd like to highlight a specific cell in my pandas dataframe. I can grab the exact position using the .loc[] function. I've tried to look at some examples using df.style.apply(lambda x: ['background color: yellow' ...] but I am not sure how to pass in the exact position I am trying to access to format.
My dataframe has a multi index so to access a specific cell I use: df.loc[(i1,i2,i3,i4,i5),col1] and this would indicate the cell I want formatted.
Thanks in advance.
For a specific cell, you can do:
# toy example
df = pd.DataFrame({'i1':[0,0,0,1,1,1],
'i2':[0,1,2,0,1,2],
'col1':[1,2,3,4,5,6]}).set_index(['i1','i2'])
subsets = pd.IndexSlice[(0,1), 'col1']
df.style.applymap(lambda x: "background-color: yellow", subset=subsets)
Which will hightlight the cell [(0,1), 'col1'].
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With