I am trying to format the color of a cell of an specific column in a data frame, but I can't manage to do it according to multiple conditions.
This is my dataframe (df):
Name ID Cel Date
0 Diego b000000005 7878 2565-05-31 20:53:00
1 Luis b000000015 6464 2017-05-11 20:53:00
2 Vidal b000000002 1100 2017-05-08 20:53:00
3 John b000000011 4545 2017-06-06 20:53:00
4 Yusef b000000013 1717 2017-06-06 20:53:00
I want the values in the "Date" column to change color according to the following conditions:
if date < datetime.now():
color = 'green'
elif date > datetime.now():
date = 'yellow'
elif date > (datetime.now() + timedelta(days=60)):
color = 'red'
This is my current code:
def color(val):
if val < datetime.now():
color = 'green'
elif val > datetime.now():
color = 'yellow'
elif val > (datetime.now() + timedelta(days=60)):
color = 'red'
return 'background-color: %s' % color
df.style.apply(color, subset = ['Fecha'])
I am getting the following error:
ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index Fecha')
The output is:
Out[65]: <pandas.formats.style.Styler at 0x1e3ab8dec50>
Any help will be appreciated.