Skip to content

Commit 5af5a2d

Browse files
committed
ENH: Implement Styler.where
1 parent 7e4e8ac commit 5af5a2d

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

pandas/io/formats/style.py

+26
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,32 @@ def applymap(self, func, subset=None, **kwargs):
623623
(func, subset), kwargs))
624624
return self
625625

626+
def where(self, func, style1, style2='', subset=None, **kwargs):
627+
"""
628+
Apply a function elementwise, updating the HTML
629+
representation with a style which is selected in
630+
accordance with the return value of a function.
631+
632+
Parameters
633+
----------
634+
func : function
635+
``func`` should take a scalar and return a boolean
636+
style1 : a style applied when ``func`` returns true
637+
style2 : a style applied when ``func`` returns false
638+
subset : IndexSlice
639+
a valid indexer to limit ``data`` to *before* applying the
640+
function. Consider using a pandas.IndexSlice
641+
kwargs : dict
642+
pass along to ``func``
643+
644+
Returns
645+
-------
646+
self : Styler
647+
648+
"""
649+
return self.applymap(lambda val: style1 if func(val) else style2,
650+
subset=subset, **kwargs)
651+
626652
def set_precision(self, precision):
627653
"""
628654
Set the precision used to render.

0 commit comments

Comments
 (0)