Skip to content

Commit 2282acd

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

File tree

1 file changed

+38
-0
lines changed

1 file changed

+38
-0
lines changed

pandas/io/formats/style.py

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

626+
def where(self, cond, value, other=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+
cond : callable
637+
``cond`` should take a scalar and return a boolean
638+
value : str
639+
applied when ``cond`` returns true
640+
other : str
641+
applied when ``cond`` 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 ``cond``
647+
648+
Returns
649+
-------
650+
self : Styler
651+
652+
See Also
653+
--------
654+
Styler.applymap
655+
656+
"""
657+
658+
if other is None:
659+
other = ''
660+
661+
return self.applymap(lambda val: value if cond(val) else other,
662+
subset=subset, **kwargs)
663+
626664
def set_precision(self, precision):
627665
"""
628666
Set the precision used to render.

0 commit comments

Comments
 (0)