Skip to content

Row based style.apply doesn't modify background colors #13657

Closed
@shri

Description

@shri

Code Sample, a copy-pastable example if possible

# add color gradient to column(s)
import numpy as np
import math
from pandas import *
from IPython.display import display, HTML

# color gradient
COLORS = ["#F1F2B5", "#CCD7A5", "#A7BC96", "#82A186", "#5D8677", "#386B67", "#135058"]

# rows/columns to skip applying gradient to
SKIPROWSORCOLUMNS = ["Day","Users"] 

# index in row or columns to skip
SKIPINDICES = [0,1]

# dataframe to operate on
df = DataFrame([["Monday",100,.65,.39,.33],["Tuesday",120,.45,.32,.31],["Wednesday",150,.75,.33,.30]])
df.columns = ["Day","Users","M1","M2","M3"] #column headers

# takes dataframe and colors
# returns colormap
def applyColorGradient(data, colors=COLORS, skiprowsorcolumns=SKIPROWSORCOLUMNS, skipindices=SKIPINDICES):
  numberOfColors = len(colors)
  i = 0
  maxNumberUnset = True
  maxNumber = 0
  while (i < len(data)):
    if (i not in skipindices):
      if(maxNumberUnset):
        maxNumber = data[i]
        maxNumberUnset = False
      else:
        current = data[i]
        if(current > maxNumber):
          maxNumber = current
    i = i + 1
  name = data.name
  dontskip = data.name not in skiprowsorcolumns
  colorArray = [colors[int(math.floor(value/maxNumber*(numberOfColors-1)))] if dontskip and index not in skipindices else '' for index,value in enumerate(data)]
  print(colorArray)
  colorSeries = pandas.Series(["background-color:" + color if color else '' for color in colorArray], name=data.name)
  display(colorSeries)
  return colorSeries


# applying colorgradient to dataframe
gradientdf = df.style.apply(applyColorGradient, axis=1) #0 is used for columns

# display table
display(gradientdf) #0 is used for columns

Expected Output

With axis = 0, this manages to give each cell a background color that is indicated. When I change this to a row based function, this doesn't color any of the cells, even though the color series are coming in. Is this a bug or am I doing something wrong?

output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.13.0-91-generic
machine: x86_64
processor:
byteorder: little
LC_ALL: en_US.UTF-8
LANG: en_US.UTF-8

pandas: 0.17.1
nose: None
pip: 8.1.2
setuptools: 23.0.0
Cython: 0.23.5
numpy: 1.10.4
scipy: 0.17.1
statsmodels: 0.6.1
IPython: 4.2.0
sphinx: None
patsy: 0.4.1
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: None
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: 1.0.13
pymysql: None
psycopg2: None
Jinja2: None

Metadata

Metadata

Assignees

No one assigned

    Labels

    DocsStylerconditional formatting using DataFrame.style

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions