Apply a CSS-styling function column-wise, row-wise, or table-wise. Updates the HTML representation with the result. Parameters ---------- func : function ``func`` should take a Series if ``axis`` in [0,1] and return a list-like objec
(
self,
func: Callable,
axis: Axis | None = 0,
subset: Subset | None = None,
**kwargs,
)
| 1982 | return self |
| 1983 | |
| 1984 | def apply( |
| 1985 | self, |
| 1986 | func: Callable, |
| 1987 | axis: Axis | None = 0, |
| 1988 | subset: Subset | None = None, |
| 1989 | **kwargs, |
| 1990 | ) -> Styler: |
| 1991 | """ |
| 1992 | Apply a CSS-styling function column-wise, row-wise, or table-wise. |
| 1993 | |
| 1994 | Updates the HTML representation with the result. |
| 1995 | |
| 1996 | Parameters |
| 1997 | ---------- |
| 1998 | func : function |
| 1999 | ``func`` should take a Series if ``axis`` in [0,1] and return a list-like |
| 2000 | object of same length, or a Series, not necessarily of same length, with |
| 2001 | valid index labels considering ``subset``. |
| 2002 | ``func`` should take a DataFrame if ``axis`` is ``None`` and return either |
| 2003 | an ndarray with the same shape or a DataFrame, not necessarily of the same |
| 2004 | shape, with valid index and columns labels considering ``subset``. |
| 2005 | axis : {0 or 'index', 1 or 'columns', None}, default 0 |
| 2006 | Apply to each column (``axis=0`` or ``'index'``), to each row |
| 2007 | (``axis=1`` or ``'columns'``), or to the entire DataFrame at once |
| 2008 | with ``axis=None``. |
| 2009 | subset : label, array-like, IndexSlice, optional |
| 2010 | A valid 2d input to `DataFrame.loc[<subset>]`, or, in the case of a 1d input |
| 2011 | or single key, to `DataFrame.loc[:, <subset>]` where the columns are |
| 2012 | prioritised, to limit ``data`` to *before* applying the function. |
| 2013 | **kwargs : dict |
| 2014 | Pass along to ``func``. |
| 2015 | |
| 2016 | Returns |
| 2017 | ------- |
| 2018 | Styler |
| 2019 | Instance of class with CSS applied to its HTML representation. |
| 2020 | |
| 2021 | See Also |
| 2022 | -------- |
| 2023 | Styler.map_index: Apply a CSS-styling function to headers elementwise. |
| 2024 | Styler.apply_index: Apply a CSS-styling function to headers level-wise. |
| 2025 | Styler.map: Apply a CSS-styling function elementwise. |
| 2026 | |
| 2027 | Notes |
| 2028 | ----- |
| 2029 | The elements of the output of ``func`` should be CSS styles as strings, in the |
| 2030 | format 'attribute: value; attribute2: value2; ...' or, |
| 2031 | if nothing is to be applied to that element, an empty string or ``None``. |
| 2032 | |
| 2033 | This is similar to ``DataFrame.apply``, except that ``axis=None`` |
| 2034 | applies the function to the entire DataFrame at once, |
| 2035 | rather than column-wise or row-wise. |
| 2036 | |
| 2037 | Examples |
| 2038 | -------- |
| 2039 | >>> def highlight_max(x, color): |
| 2040 | ... return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};", None) |
| 2041 | >>> df = pd.DataFrame(np.random.randn(5, 2), columns=["A", "B"]) |
no test coverage detected