Python 基于某种条件的彩色数据帧

Python 基于某种条件的彩色数据帧,python,pandas,colors,Python,Pandas,Colors,我想根据下面的条件给数据框上色 LL UL colu_1 colu_2 colu 3 colu 4 1 0 10 5 -6 13 46 2 3 12 0 5 8 55 3楠楠-9 8 4 5 如果每行中的值低于列LL或高于列UL,我想对col\u 1col\u 2col\u 3和col\u 4进行着色 如果较低,则将其涂成红色。如果更高,则将其涂成绿色 此外,如果没有上限和下限,则在该行中不

我想根据下面的条件给数据框上色

LL UL colu_1 colu_2 colu 3 colu 4
1   0    10     5       -6      13      46
2   3    12     0        5       8      55
3楠楠-9 8 4 5
如果每行中的值低于列
LL
或高于列
UL
,我想对
col\u 1
col\u 2
col\u 3
col\u 4
进行着色

如果较低,则将其涂成红色。如果更高,则将其涂成绿色

此外,如果没有上限和下限,则在该行中不执行任何操作

谢谢

结果可能是这样的

通过
LL
UL
列比较所有列,返回样式数据框由以下内容填充:



您在代码中尝试了什么来实现同样的效果?我使用了
df.style.apply(highlight,axis=1)
,但我不知道如何在highlight函数中编写条件代码。Dupe错误,因此重新打开,解决方案更复杂
def highlight(x):
    c1 = 'background-color: red'
    c2 = 'background-color: green'
    c3 = '' 
    m1 = x.lt(x['LL'], axis=0)
    m2 = x.gt(x['UL'], axis=0)
    #if necessary set first 2 columns to False
    m1.iloc[:, :2] = False
    m2.iloc[:, :2] = False

    out = np.select([m1, m2], [c1, c2], default=c3)
    return pd.DataFrame(out, index=x.index, columns=x.columns)

df.style.apply(highlight, axis=None)
df.style.apply(highlight, axis=None).to_excel('file.xlsx', index=False)