5

I have one pandas dataframe that I want to style the format based on the values of another dataframe of the same shape/size. I'm trying to use applymap.

Here's an example:

t1= pd.DataFrame({'x':['A','B','C'], 'y':['C','B','D']})
t2= pd.DataFrame({'x':[0.3,0.2,0.7], 'y':[1,0.3,2]})

def color_cells(s, threshold=0.5):
    if s > threshold:
        return 'color:{0}; font-weight:bold'.format('red')
    else:
        return ''

#Tried
t1.style.applymap(t2.applymap(color_cells))

Ideally in t1 where the corresponding cells in t2>0.5 then the values in t1 are in 'red-bold'.

However, I'm unsure what pattern I should use to get this desired effect.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92
Den Thap
  • 153
  • 5

1 Answers1

8

You were almost there, you need to use the apply function with a lambda instead to iterate through the cells.

t1.style.apply(lambda x: t2.applymap(color_cells), axis=None)

Yash Mehra
  • 169
  • 1
  • 6