diff --git a/doc/source/user_guide/style.ipynb b/doc/source/user_guide/style.ipynb index 12dd72f761408..24f344488d1ca 100644 --- a/doc/source/user_guide/style.ipynb +++ b/doc/source/user_guide/style.ipynb @@ -793,7 +793,8 @@ "source": [ "The next option you have are \"table styles\".\n", "These are styles that apply to the table as a whole, but don't look at the data.\n", - "Certain stylings, including pseudo-selectors like `:hover` can only be used this way." + "Certain stylings, including pseudo-selectors like `:hover` can only be used this way.\n", + "These can also be used to set specific row or column based class selectors, as will be shown." ] }, { @@ -831,9 +832,32 @@ "The value for `props` should be a list of tuples of `('attribute', 'value')`.\n", "\n", "`table_styles` are extremely flexible, but not as fun to type out by hand.\n", - "We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here." + "We hope to collect some useful ones either in pandas, or preferable in a new package that [builds on top](#Extensibility) the tools here.\n", + "\n", + "`table_styles` can be used to add column and row based class descriptors. For large tables this can increase performance by avoiding repetitive individual css for each cell, and it can also simplify style construction in some cases.\n", + "If `table_styles` is given as a dictionary each key should be a specified column or index value and this will map to specific class CSS selectors of the given column or row.\n", + "\n", + "Note that `Styler.set_table_styles` will overwrite existing styles but can be chained by setting the `overwrite` argument to `False`." ] }, + { + "cell_type": "code", + "execution_count": null, + "outputs": [], + "source": [ + "html = html.set_table_styles({\n", + " 'B': [dict(selector='', props=[('color', 'green')])],\n", + " 'C': [dict(selector='td', props=[('color', 'red')])], \n", + " }, overwrite=False)\n", + "html" + ], + "metadata": { + "collapsed": false, + "pycharm": { + "name": "#%%\n" + } + } + }, { "cell_type": "markdown", "metadata": {}, @@ -922,10 +946,12 @@ "- DataFrame only `(use Series.to_frame().style)`\n", "- The index and columns must be unique\n", "- No large repr, and performance isn't great; this is intended for summary DataFrames\n", - "- You can only style the *values*, not the index or columns\n", + "- You can only style the *values*, not the index or columns (except with `table_styles` above)\n", "- You can only apply styles, you can't insert new HTML entities\n", "\n", - "Some of these will be addressed in the future.\n" + "Some of these will be addressed in the future.\n", + "Performance can suffer when adding styles to each cell in a large DataFrame.\n", + "It is recommended to apply table or column based styles where possible to limit overall HTML length, as well as setting a shorter UUID to avoid unnecessary repeated data transmission. \n" ] }, { diff --git a/doc/source/whatsnew/v1.2.0.rst b/doc/source/whatsnew/v1.2.0.rst index 727b5ec92bdc4..ce0245036aded 100644 --- a/doc/source/whatsnew/v1.2.0.rst +++ b/doc/source/whatsnew/v1.2.0.rst @@ -232,6 +232,7 @@ Other enhancements - :class:`Index` with object dtype supports division and multiplication (:issue:`34160`) - :meth:`DataFrame.explode` and :meth:`Series.explode` now support exploding of sets (:issue:`35614`) - :meth:`DataFrame.hist` now supports time series (datetime) data (:issue:`32590`) +- :meth:`Styler.set_table_styles` now allows the direct styling of rows and columns and can be chained (:issue:`35607`) - ``Styler`` now allows direct CSS class name addition to individual data cells (:issue:`36159`) - :meth:`Rolling.mean()` and :meth:`Rolling.sum()` use Kahan summation to calculate the mean to avoid numerical problems (:issue:`10319`, :issue:`11645`, :issue:`13254`, :issue:`32761`, :issue:`36031`) - :meth:`DatetimeIndex.searchsorted`, :meth:`TimedeltaIndex.searchsorted`, :meth:`PeriodIndex.searchsorted`, and :meth:`Series.searchsorted` with datetimelike dtypes will now try to cast string arguments (listlike and scalar) to the matching datetimelike type (:issue:`36346`) diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 4b7a5e76cb475..638b4b1aed774 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -991,20 +991,46 @@ def set_caption(self, caption: str) -> "Styler": self.caption = caption return self - def set_table_styles(self, table_styles) -> "Styler": + def set_table_styles(self, table_styles, axis=0, overwrite=True) -> "Styler": """ Set the table styles on a Styler. These are placed in a ``