Reputation: 47
So I have Pandas DF like:
name sal
--------------
Piter 12345
Pen 10450
Paul 99995
Punk 54001
Pok 45555
By matching text like '45' (only in column 'sal', string type) I want text in it got red.
So in my case this text should be highlighted in red:
12345
10450
45555
So I know how to highlight max value or min value, I also can "if val in (45, '45')", but this only match by whole text in cell. I googled for "df.str.contains('45', regex=True)", but I something don't know.
My code is:
def color_in(val):
if val <like '45' or something, plese help>:
color = 'red'
else:
color = 'black'
return 'color: %s' % color
s = df.style.applymap(color_in, subset=['sal'])
s
Upvotes: 2
Views: 2805
Reputation: 30971
Another possible solution, without the need to define another function:
df.style.where(lambda val: '45' in str(val), 'color: red', subset=['sal'])
Upvotes: 1
Reputation: 862511
Use in
opeartor, because working with scalars, if values are numeric change val
to str(val)
for cast to string:
def color_in(val):
if '45' in val:
color = 'red'
else:
color = 'black'
return 'color: %s' % color
Upvotes: 1