Skip to content

Commit b798cf4

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

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

pandas/io/formats/style.py

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

626+
def where(self, func, style1, style2=None, 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+
.. versionadded:: 0.21.0
633+
634+
Parameters
635+
----------
636+
func : callable
637+
``func`` should take a scalar and return a boolean
638+
style1 : str
639+
applied when ``func`` returns true
640+
style2 : str
641+
applied when ``func`` returns false
642+
subset : IndexSlice
643+
a valid indexer to limit ``data`` to *before* applying the
644+
function. Consider using a pandas.IndexSlice
645+
kwargs : dict
646+
pass along to ``func``
647+
648+
Returns
649+
-------
650+
self : Styler
651+
652+
"""
653+
654+
if style2 is None:
655+
style2 = ''
656+
657+
return self.applymap(lambda val: style1 if func(val) else style2,
658+
subset=subset, **kwargs)
659+
626660
def set_precision(self, precision):
627661
"""
628662
Set the precision used to render.

0 commit comments

Comments
 (0)