df.style
我想知道如何使用方法突出显示熊猫数据框的对角线元素。
我找到了这个官方链接,他们在其中讨论如何突出最大值,但我很难创建突出对角线元素的功能。
这是一个例子:
import numpy as np
import pandas as pd
df = pd.DataFrame({'a':[1,2,3,4],'b':[1,3,5,7],'c':[1,4,7,10],'d':[1,5,9,11]})
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]
df.style.apply(highlight_max)
我只想在对角线元素 1、3、7、11 上突出显示黄色。
怎么做?