pandas.io.formats.style.Styler.apply¶
- Styler.apply(func, axis=0, subset=None, **kwargs)[source]¶
- Apply a CSS-styling function column-wise, row-wise, or table-wise. - Updates the HTML representation with the result. - Parameters
- funcfunction
- funcshould take a Series if- axisin [0,1] and return an object of same length, also with identical index if the object is a Series.- funcshould take a DataFrame if- axisis- Noneand return either an ndarray with the same shape or a DataFrame with identical columns and index.- Changed in version 1.3.0. 
- axis{0 or ‘index’, 1 or ‘columns’, None}, default 0
- Apply to each column ( - axis=0or- 'index'), to each row (- axis=1or- 'columns'), or to the entire DataFrame at once with- axis=None.
- 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 - datato before applying the function.
- **kwargsdict
- Pass along to - func.
 
- Returns
- selfStyler
 
 - See also - Styler.applymap
- Apply a CSS-styling function elementwise. 
 - Notes - The elements of the output of - funcshould 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.- This is similar to - DataFrame.apply, except that- axis=Noneapplies the function to the entire DataFrame at once, rather than column-wise or row-wise.- Examples - >>> def highlight_max(x, color): ... return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};", None) >>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"]) >>> df.style.apply(highlight_max, color='red') >>> df.style.apply(highlight_max, color='blue', axis=1) >>> df.style.apply(highlight_max, color='green', axis=None) - Using - subsetto restrict application to a single column or multiple columns- >>> df.style.apply(highlight_max, color='red', subset="A") >>> df.style.apply(highlight_max, color='red', subset=["A", "B"]) - Using a 2d input to - subsetto select rows in addition to columns- >>> df.style.apply(highlight_max, color='red', subset=([0,1,2], slice(None)) >>> df.style.apply(highlight_max, color='red', subset=(slice(0,5,2), "A")