title: How do I style a subset of a pandas dataframe?
body: <p>I previously asked <a href="https://stackoverflow.com/questions/56942320/how-do-i-style-only-the-last-row-of-a-pandas-dataframe">How do I style only the last row of a pandas dataframe?</a> and got a perfect answer to the toy problem that I gave. </p>
  
  <p>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):</p>
  
  <pre class="lang-py prettyprint-override"><code>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)
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/kKu7l.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/kKu7l.png" alt="styling applied to all numeric data"></a></p>
  
  <p>However, like the previous question, <strong>I wish to only apply this styling to the last row</strong>. The answer to the previous question was: </p>
  
  <pre class="lang-py prettyprint-override"><code>df.style.background_gradient(cmap=cm, subset=df.index[-1])
  </code></pre>
  
  <p>which in this case gives the error: </p>
  
  <pre><code>---------------------------------------------------------------------------
  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:
  --&gt; 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         """
  --&gt; 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         """
  --&gt; 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:
  --&gt; 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,
  --&gt; 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)
  -&gt; 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 
  --&gt; 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
  --&gt; 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):
  --&gt; 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):
  ---&gt; 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()
  --&gt; 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')
  
  &lt;pandas.io.formats.style.Styler at 0x7f948dde7278&gt;
  </code></pre>
  
  <p>which seems to come from the fact that it's trying to do an operation to strings in the <code>text_column</code>. 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 <code>subset</code> method.</p>
  
  <p>I am running:</p>
  
  <pre><code>python version 3.7.3 
  pandas version 0.24.2
  </code></pre>