我通常使用熊猫在工作簿中生成美观的列表输出
如何对每行应用不同的条件格式以突出显示失败的单元格。即R1行的值> 3,R2行的值<5等等
或者说...对于每列,R2 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))
答案 0 :(得分:3)
我相信您需要使用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
: