pandas.io.formats.style.Styler.set_td_classes

Styler.set_td_classes(classes)[source]

Set the DataFrame of strings added to the class attribute of <td> HTML elements.

Parameters
classes:DataFrame

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
self:Styler

See also

Styler.set_table_styles

Set the table styles included within the <style> HTML element.

Styler.set_table_attributes

Set the table attributes added to the <table> HTML element.

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,

>>> df = pd.DataFrame([[1]])
>>> css = pd.DataFrame([["other-class"]])
>>> s = Styler(df, uuid="_", cell_ids=False).set_td_classes(css)
>>> s.hide_index().render()
'<style type="text/css"></style>'
'<table id="T__">'
'  <thead>'
'    <tr><th class="col_heading level0 col0" >0</th></tr>'
'  </thead>'
'  <tbody>'
'    <tr><td class="data row0 col0 other-class" >1</td></tr>'
'  </tbody>'
'</table>'

© 2008–2021, AQR Capital Management, LLC, Lambda Foundry, Inc. and PyData Development Team
Licensed under the 3-clause BSD License.
https://pandas.pydata.org/pandas-docs/version/1.3.4/reference/api/pandas.io.formats.style.Styler.set_td_classes.html