0

Trying to change colors of positive and negative values Keep getting: 'Styler' object has no attribute 'style' I need to be column 'Spread' to be affected Checked the answered threads for solutions and just copied and it makes sense but still keep getting error

    Date    Open    Close   Volume  Weekday LastFriday  Sold Contract   Spread
231 2022-10-07 00:00:00-04:00   64.010002   58.439999   163577800   Friday  63.360001   Expired -4.920002
236 2022-10-14 00:00:00-04:00   59.599998   55.939999   96831500    Friday  58.439999   Expired -2.500000
241 2022-10-21 00:00:00-04:00   57.220001   58.820000   95295600    Friday  55.939999   ITM 2.880001
246 2022-10-28 00:00:00-04:00   59.099998   62.009998   78198600    Friday  58.820000   ITM 3.189999
251 2022-11-04 00:00:00-04:00   62.500000   62.189999   84751100    Friday  62.009998   ITM 0.180000

What i tried and it seems correct

def color_negative_red(value):

  if value < 0:
    color = 'red'
  elif value > 0:
    color = 'green'
  else:
    color = 'black'

  return 'color: %s' % color

df.style.applymap(color_negative_red, subset=['Spread'])

Output: 'Styler' object has no attribute 'style'

SOLVED WITH:

f = lambda x: "color: red" if x<0 else "color: green"

df.style.applymap(f, subset=['Spread']) #color the values if Sold contract was ITM or Expired <

russki
  • 13
  • 3

0 Answers0