'How do I style a subset of a pandas dataframe?

I previously asked How do I style only the last row of a pandas dataframe? and got a perfect answer to the toy problem that I gave.

Turns out I should have made the toy problem a bit closer to my real problem. Consider a dataframe with more than 1 column of text data (which I can apply styling to):

import pandas as pd
import numpy as np
import seaborn as sns
cm = sns.diverging_palette(-5, 5, as_cmap=True)

df = pd.DataFrame(np.random.randn(3, 4))
df['text_column'] = 'a'
df['second_text_column'] = 'b'
df.style.background_gradient(cmap=cm)

styling applied to all numeric data

However, like the previous question, I wish to only apply this styling to the last row. The answer to the previous question was:

df.style.background_gradient(cmap=cm, subset=df.index[-1])

which in this case gives the error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/usr/local/miniconda/lib/python3.7/site-packages/IPython/core/formatters.py in __call__(self, obj)
    343             method = get_real_method(obj, self.print_method)
    344             if method is not None:
--> 345                 return method()
    346             return None
    347         else:

/usr/local/miniconda/lib/python3.7/site-packages/pandas/io/formats/style.py in _repr_html_(self)
    161         Hooks into Jupyter notebook rich display system.
    162         """
--> 163         return self.render()
    164 
    165     @Appender(_shared_docs['to_excel'] % dict(

/usr/local/miniconda/lib/python3.7/site-packages/pandas/io/formats/style.py in render(self, **kwargs)
    457         * table_attributes
    458         """
--> 459         self._compute()
    460         # TODO: namespace all the pandas keys
    461         d = self._translate()

/usr/local/miniconda/lib/python3.7/site-packages/pandas/io/formats/style.py in _compute(self)
    527         r = self
    528         for func, args, kwargs in self._todo:
--> 529             r = func(self)(*args, **kwargs)
    530         return r
    531 

/usr/local/miniconda/lib/python3.7/site-packages/pandas/io/formats/style.py in _apply(self, func, axis, subset, **kwargs)
    536         if axis is not None:
    537             result = data.apply(func, axis=axis,
--> 538                                 result_type='expand', **kwargs)
    539             result.columns = data.columns
    540         else:

/usr/local/miniconda/lib/python3.7/site-packages/pandas/core/frame.py in apply(self, func, axis, broadcast, raw, reduce, result_type, args, **kwds)
   6485                          args=args,
   6486                          kwds=kwds)
-> 6487         return op.get_result()
   6488 
   6489     def applymap(self, func):

/usr/local/miniconda/lib/python3.7/site-packages/pandas/core/apply.py in get_result(self)
    149             return self.apply_raw()
    150 
--> 151         return self.apply_standard()
    152 
    153     def apply_empty_result(self):

/usr/local/miniconda/lib/python3.7/site-packages/pandas/core/apply.py in apply_standard(self)
    255 
    256         # compute the result using the series generator
--> 257         self.apply_series_generator()
    258 
    259         # wrap results

/usr/local/miniconda/lib/python3.7/site-packages/pandas/core/apply.py in apply_series_generator(self)
    284             try:
    285                 for i, v in enumerate(series_gen):
--> 286                     results[i] = self.f(v)
    287                     keys.append(v.name)
    288             except Exception as e:

/usr/local/miniconda/lib/python3.7/site-packages/pandas/core/apply.py in f(x)
     76 
     77             def f(x):
---> 78                 return func(x, *args, **kwds)
     79         else:
     80             f = func

/usr/local/miniconda/lib/python3.7/site-packages/pandas/io/formats/style.py in _background_gradient(s, cmap, low, high, text_color_threshold)
    941             smin = s.values.min()
    942             smax = s.values.max()
--> 943             rng = smax - smin
    944             # extend lower / upper bounds, compresses color range
    945             norm = colors.Normalize(smin - (rng * low), smax + (rng * high))

TypeError: ("unsupported operand type(s) for -: 'str' and 'str'", 'occurred at index text_column')

<pandas.io.formats.style.Styler at 0x7f948dde7278>

which seems to come from the fact that it's trying to do an operation to strings in the text_column. Fair enough. How do I tell it to only apply to the last row for all non-text columns? I'm ok with giving it explicit column names to use or avoid, but I don't know how to pass that into this inscrutable subset method.

I am running:

python version 3.7.3 
pandas version 0.24.2


Solution 1:[1]

You want to apply a style on a pandas dataframe and set different colors on differents columns or lines.
Here you can find a code ready to run on your own df. :)
Apply on lines using the axis = 0 and the subset on the df.index or as in this exemple on the columns axis=1 and the subset on the df.columns

cmaps = [
    'Greys', 'Purples', 'Blues', 'Greens', 'Oranges', 'Reds',
    'YlOrBr', 'YlOrRd', 'OrRd', 'PuRd', 'RdPu', 'BuPu',
    'GnBu', 'PuBu', 'YlGnBu', 'PuBuGn', 'BuGn', 'YlGn'
]

df.style.\
    background_gradient(
        cmap=cmaps[1], axis=0
        subset= (
            df.index[:],
            df.columns[df.columns.get_loc('nb tickets'):df.columns.get_loc('nb ref_prod')+1]
        )
    ).\
    background_gradient(
        cmap=cmaps[3],
        subset= (
            df.index[:],
            df.columns[df.columns.get_loc('am'):df.columns.get_loc('pm')+1]
        )
    ).\
    background_gradient(
        cmap=cmaps[4],
        subset= (
            df.index[:],
            df.columns[df.columns.get_loc('Week_1'):df.columns.get_loc('Week_5')+1]
        )
    ).\
    background_gradient(
        cmap=cmaps[5],
        subset= (
            df.index[:],
            df.columns[df.columns.get_loc('sum qty'):df.columns.get_loc('sum euro')+1]
        )
    )

enter image description here

Sources

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

Source: Stack Overflow

Solution Source
Solution 1