Skip to content

Commit f802626

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

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

pandas/io/formats/style.py

+37
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,43 @@ 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+
if other is None:
658+
other = ''
659+
660+
return self.applymap(lambda val: value if cond(val) else other,
661+
subset=subset, **kwargs)
662+
626663
def set_precision(self, precision):
627664
"""
628665
Set the precision used to render.

0 commit comments

Comments
 (0)