@@ -933,15 +933,23 @@ def set_table_styles(self, table_styles, axis=0, overwrite=True) -> "Styler":
933
933
in their respective tuple form. The dict values should be
934
934
a list as specified in the form with CSS selectors and
935
935
props that will be applied to the specified row or column.
936
+
937
+ .. versionchanged:: 1.2.0
938
+
936
939
axis : {0 or 'index', 1 or 'columns', None}, default 0
937
940
Apply to each column (``axis=0`` or ``'index'``), to each row
938
941
(``axis=1`` or ``'columns'``). Only used if `table_styles` is
939
942
dict.
943
+
944
+ .. versionadded:: 1.2.0
945
+
940
946
overwrite : boolean, default True
941
947
Styles are replaced if `True`, or extended if `False`. CSS
942
948
rules are preserved so most recent styles set will dominate
943
949
if selectors intersect.
944
950
951
+ .. versionadded:: 1.2.0
952
+
945
953
Returns
946
954
-------
947
955
self : Styler
@@ -954,33 +962,38 @@ def set_table_styles(self, table_styles, axis=0, overwrite=True) -> "Styler":
954
962
... [{'selector': 'tr:hover',
955
963
... 'props': [('background-color', 'yellow')]}]
956
964
... )
965
+
966
+ Adding column styling by name
967
+
957
968
>>> df.style.set_table_styles({
958
969
... 'A': [{'selector': '',
959
970
... 'props': [('color', 'red')]}],
960
971
... 'B': [{'selector': 'td',
961
972
... 'props': [('color', 'blue')]}]
962
973
... }, overwrite=False)
974
+
975
+ Adding row styling
976
+
963
977
>>> df.style.set_table_styles({
964
978
... 0: [{'selector': 'td:hover',
965
979
... 'props': [('font-size', '25px')]}]
966
980
... }, axis=1, overwrite=False)
967
981
"""
968
- if isinstance (table_styles , dict ):
969
- if axis == 0 or axis == "index" :
970
- obj = self .data .columns
971
- idf = ".col"
982
+ if is_dict_like (table_styles ):
983
+ if axis in [0 , "index" ]:
984
+ obj , idf = self .data .columns , ".col"
972
985
else :
973
- obj = self .data .index
974
- idf = ".row"
975
-
976
- _styles = []
977
- for key , styles in table_styles . items ():
978
- for s in styles :
979
- c = str ( obj . get_loc ( key ))
980
- _styles . append (
981
- { "selector" : s [ "selector" ] + idf + c , "props" : s [ "props" ]}
982
- )
983
- table_styles = _styles
986
+ obj , idf = self .data .index , ".row"
987
+
988
+ table_styles = [
989
+ {
990
+ "selector" : s [ "selector" ] + idf + str ( obj . get_loc ( key )),
991
+ "props" : s [ "props" ],
992
+ }
993
+ for key , styles in table_styles . items ()
994
+ for s in styles
995
+ ]
996
+
984
997
if not overwrite and self .table_styles is not None :
985
998
self .table_styles .extend (table_styles )
986
999
else :
0 commit comments