data = {
'Month' : [1, 2, 3],
'Temp' : [7, -18, -20]
}
{'Month': [1, 2, 3], 'Temp': [7, -18, -20]}
dict
|
Month |
Temp |
0 |
1 |
7 |
1 |
2 |
-18 |
2 |
3 |
-20 |
def color_negative_red(val):
"""
Takes a scalar and returns a string with
the css property `'color: red'` for negative
strings, black otherwise.
"""
color = 'red' if val < 0 else 'black'
return 'color: %s' % color
df.style.applymap(color_negative_red)
| Month | Temp |
0 |
1 |
7 |
1 |
2 |
-18 |
2 |
3 |
-20 |
|
Month |
Temp |
0 |
1 |
7 |
1 |
2 |
-18 |
2 |
3 |
-20 |
# Highlight max (axis = 0)
# https://pandas.pydata.org/pandas-docs/stable/user_guide/style.html
df.style.highlight_max(axis = 0)
| Month | Temp |
0 |
1 |
7 |
1 |
2 |
-18 |
2 |
3 |
-20 |
def highlight_max_rj(s, color = 'lightgreen'):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: '+color if v else '' for v in is_max]
df.style.apply(highlight_max_rj, color = 'lightblue', axis = 0, subset=['Temp'])
| Month | Temp |
0 |
1 |
7 |
1 |
2 |
-18 |
2 |
3 |
-20 |
df.style.apply(highlight_max_rj, axis = 1, subset = ['Temp'])
| Month | Temp |
0 |
1 |
7 |
1 |
2 |
-18 |
2 |
3 |
-20 |