pandas.io.formats.style.Styler.set_td_classes — pandas 2.2.3 documentation (original) (raw)

Styler.set_td_classes(classes)[source]#

Set the class attribute of <td> HTML elements.

Parameters:

classesDataFrame

DataFrame containing strings that will be translated to CSS classes, mapped by identical column and index key values that must exist on the underlying Styler data. None, NaN values, and empty strings will be ignored and not affect the rendered HTML.

Returns:

Styler

Notes

Can be used in combination with Styler.set_table_styles to define an internal CSS solution without reference to external CSS files.

Examples

df = pd.DataFrame(data=[[1, 2, 3], [4, 5, 6]], columns=["A", "B", "C"]) classes = pd.DataFrame([ ... ["min-val red", "", "blue"], ... ["red", None, "blue max-val"] ... ], index=df.index, columns=df.columns) df.style.set_td_classes(classes)

Using MultiIndex columns and a classes DataFrame as a subset of the underlying,

df = pd.DataFrame([[1,2],[3,4]], index=["a", "b"], ... columns=[["level0", "level0"], ["level1a", "level1b"]]) classes = pd.DataFrame(["min-val"], index=["a"], ... columns=[["level0"],["level1a"]]) df.style.set_td_classes(classes)

Form of the output with new additional css classes,

from pandas.io.formats.style import Styler df = pd.DataFrame([[1]]) css = pd.DataFrame([["other-class"]]) s = Styler(df, uuid="_", cell_ids=False).set_td_classes(css) s.hide(axis=0).to_html()
'' '

' ' ' ' ' ' ' ' ' ' ' ' ' '
0
1
'