Styler.
set_td_classes
Add string based CSS class names to data cells that will appear within the Styler HTML result. These classes are added within specified <td> elements.
DataFrame containing strings that will be translated to CSS classes, mapped by identical column and index values that must exist on the underlying Styler data. None, NaN values, and empty strings will be ignored and not affect the rendered HTML.
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>'