pandas.io.formats.style.Styler.applymap#

Styler.applymap(func, subset=None, **kwargs)[source]#

Apply a CSS-styling function elementwise.

Updates the HTML representation with the result.

Parameters
funcfunction

func should take a scalar and return a string.

subsetlabel, array-like, IndexSlice, optional

A valid 2d input to DataFrame.loc[<subset>], or, in the case of a 1d input or single key, to DataFrame.loc[:, <subset>] where the columns are prioritised, to limit data to before applying the function.

**kwargsdict

Pass along to func.

Returns
selfStyler

See also

Styler.applymap_index

Apply a CSS-styling function to headers elementwise.

Styler.apply_index

Apply a CSS-styling function to headers level-wise.

Styler.apply

Apply a CSS-styling function column-wise, row-wise, or table-wise.

Notes

The elements of the output of func should be CSS styles as strings, in the format ‘attribute: value; attribute2: value2; …’ or, if nothing is to be applied to that element, an empty string or None.

Examples

>>> def color_negative(v, color):
...     return f"color: {color};" if v < 0 else None
>>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"])
>>> df.style.applymap(color_negative, color='red')  

Using subset to restrict application to a single column or multiple columns

>>> df.style.applymap(color_negative, color='red', subset="A")
...  
>>> df.style.applymap(color_negative, color='red', subset=["A", "B"])
...  

Using a 2d input to subset to select rows in addition to columns

>>> df.style.applymap(color_negative, color='red',
...  subset=([0,1,2], slice(None)))  
>>> df.style.applymap(color_negative, color='red', subset=(slice(0,5,2), "A"))
...  

See Table Visualization user guide for more details.