对不同的行应用不同的条件格式
Apply different conditional formatting to different rows
我通常使用 pandas 在工作簿中生成漂亮的表格输出
如何对每行应用不同的条件格式来突出显示失败的单元格。即对于 R1 行,值 > 3,R2 行值 <5 等...
或者说...对于每一列,R2 < R1
R1 = [1,10,3]
R2 = [4,5,6]
E_Rfsw = [7,8,9]
data = [R1, R2,E_Rfsw]
df = pd.DataFrame(data,
columns=[V for V in design.Vout],
index=['FB R1 (kΩ)', 'FB R2 (kΩ)','Fsw resistor (kΩ)'],
)
display(df.round(4))
我相信您需要 Styler.apply
:
的自定义函数
def highlight_row(x):
c1 = 'background-color: red'
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#filter index by R1, R2
m1 = x.index.str.contains('R1')
m2 = x.index.str.contains('R2')
#compare filtered rows by conditions and add Falses for all rows
mask = (x[m1] > 3).append((x[m2] > 5)).reindex(x.index, fill_value=False)
#set values by mask
df1 = df1.mask(mask, c1)
return df1
df.style.apply(highlight_row, axis=None)
示例数据 - 仅删除了列参数,因为未指定 design.Vout
:
我通常使用 pandas 在工作簿中生成漂亮的表格输出
如何对每行应用不同的条件格式来突出显示失败的单元格。即对于 R1 行,值 > 3,R2 行值 <5 等...
或者说...对于每一列,R2 < R1
R1 = [1,10,3]
R2 = [4,5,6]
E_Rfsw = [7,8,9]
data = [R1, R2,E_Rfsw]
df = pd.DataFrame(data,
columns=[V for V in design.Vout],
index=['FB R1 (kΩ)', 'FB R2 (kΩ)','Fsw resistor (kΩ)'],
)
display(df.round(4))
我相信您需要 Styler.apply
:
def highlight_row(x):
c1 = 'background-color: red'
df1 = pd.DataFrame('', index=x.index, columns=x.columns)
#filter index by R1, R2
m1 = x.index.str.contains('R1')
m2 = x.index.str.contains('R2')
#compare filtered rows by conditions and add Falses for all rows
mask = (x[m1] > 3).append((x[m2] > 5)).reindex(x.index, fill_value=False)
#set values by mask
df1 = df1.mask(mask, c1)
return df1
df.style.apply(highlight_row, axis=None)
示例数据 - 仅删除了列参数,因为未指定 design.Vout
: