'Comparing and coloring 2 cells in the same row pandas

I am trying to compare 2 cells in pandas. To do this:

import pandas as pd
from pandas_datareader import data as web
df = web.DataReader('fb', 'yahoo')

With this lambda function to get the dates where df['Open'] > df['Close']:

df.apply(lambda x: x['Close'] if x['Close'] <=
                     x['Open'] else np.nan, axis=1)

I want to highlight df['Close'] cell with red once this condition is met:

df.style.applymap(lambda x: 'background-color : red' if x['Close'] <=
                     x['Open'] else 'background-color : green')

But this is giving me this error:

TypeError: 'float' object is not subscriptable

Could you please advise why is it giving this error?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source