我有一个这样的数据框(df
),使用颜色图进行样式设置:
#create random 30 x 30 frame
df = pd.DataFrame(np.random.randint(0, 100, (5, 20)))
df.style.background_gradient(cmap='RdYlGn_r')
上面的代码为所有数字上的数据框着色(5 x 20 单元格 - 绿色数字较小,红色数字较大)。
我如何为单独考虑的每一行(不是作为一组 5 x 20 单元格)从小到大着色,即单独考虑第 0 到 4 行的 1 行 x 20 列。
===
以上面的以下 2 个示例为例,分别使用 apply按行和列df
突出显示中位数。对于从小到大的数字,我如何为每一行上色。
def highlight_max(s):
'''
highlight the maximum in a Series yellow.
'''
is_max = s == s.max()
return ['background-color: yellow' if v else '' for v in is_max]
display(
HTML("""<p style="background-color:lightblue;color:black;font-weight: bold">
each row - median highlight
</p>""")
)
display(df.head(5).style.apply(highlight_max, axis=1))
display(
HTML("""<p style="background-color:lightblue;color:black;font-weight: bold">
each col - median highlight
</p>""")
)
display(df.head(5).style.apply(highlight_max, axis=0))