Color Only Some Value(s) In A List In Pandas Dataframe
I have a dataframe each cell in one of the columns contains a list in the format [2,19,25,39,49]. I would like to color individual values in each list that are contained in a list
Solution 1:
Convert common (your list) to a regex pattern:
pat = re.compile(r'\b(' + '|'.join(map(str, common)) + r')\b')
(import re required).
Then define the following formatting function:
defmyFmt(txt):
return pat.sub(r'<font color="#ff0000">\1</font>', repr(txt))
(I assumed that the required formatting is "color red", but change it according to what you want.)
And to present your DataFrame formatted your way, run:
df.style.format(myFmt)
To test it, I created a DataFrame containing:
AB0[1, 9, 25][10, 18, 77]1[3, 7, 22][4, 21, 27]2[11, 16, 29][24, 38, 41]
and got the following result:
Note: If you want to apply this formatting to selected columns only, pass subset parameter with a list of "wanted" columns.
Post a Comment for "Color Only Some Value(s) In A List In Pandas Dataframe"