color only some value(s) in a list in pandas dataframe

Question:

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 common = [7,9,16,18,19,20,21,25,33,35,38,40,49], so in the example 19,25 and 49 should be colored differently or that is what I would like to achieve.
I have tried using:

def color_common(x):
   if x in common:
     color='green'
   else:
     color='black'

I get the response, "The truth value of a Series is ambiguous".
if I use x.any(), I get "Result has shape: (6,)
Expected shape: (5, 6)"
I don’t understand the expected shape comment nor the ambiguous truth and where/how to apply any() or all().
Is it possibile to color components of a list.I manage to color whole cells or the contents of a cell quite easily. I have not styled a Panda dataframe before hence possibily the ‘dumbness of my question

Asked By: user1478335

||

Answers:

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:

def myFmt(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:

              A             B
0    [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:

enter image description here

Note: If you want to apply this formatting to selected columns only,
pass subset parameter with a list of "wanted" columns.

Answered By: Valdi_Bo