0

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

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
user1478335
  • 1,769
  • 5
  • 25
  • 37
  • sorry command is results.style.apply(color_common) – user1478335 Sep 08 '20 at 09:07
  • I have tried using ,without success , the following to solve the ambiguity: def color_common(x): if x in common: for i in range(len(results.iloc[0][0])): for num in results.iloc[i][0]: if x == num: color='green' else: color='black' – user1478335 Sep 08 '20 at 12:29

1 Answers1

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:

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.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • Thank you. Works brilliantly. Not sure how to pass the subset parameter. I will need to think that out. – user1478335 Sep 08 '20 at 12:36
  • *subset* is a parameter of *df.style.format*. Try e.g. *df.style.format(myFmt, subset=['A'])* to format only column "A". – Valdi_Bo Sep 08 '20 at 13:34
  • Works exactly as I had hoped I could achieve. I would have not got there without your information and solution – user1478335 Sep 08 '20 at 14:10