我试图在风格上突出熊猫的主要对角线和相反的主要对角线元素 数据帧
我看到了这个链接:Pandas style: How to highlight diagonal elements
这显示了如何突出主对角线,但我想知道如何突出两个对角线与 两种不同的颜色
这是我的数据框:
import numpy as np
import pandas as pd
df = pd.DataFrame(data={'pred0': [10, 4],
'pred1': [0, 0],
'total': [10, 4]},index=['true0','true1']
)
print(df)
pred0 pred1 total
true0 10 0 10
true1 4 0 4
# Credit: ALLOLZ
def highlight_diag(df):
a = np.full(df.shape, '', dtype='<U24')
np.fill_diagonal(a, 'background-color: yellow')
return pd.DataFrame(a, index=df.index, columns=df.columns)
df.style.apply(highlight_diag, axis=None)
但这只会高亮显示一条对角线,而不会高亮显示另一条对角线。 如何突出显示两条对角线
pred0 pred1 total
true0 10(green) 0(red) 10(no highlight)
true1 4(red) 0(green) 4(no highlight)
蒂伊
这就是你想要的
相关问题 更多 >
编程相关推荐