Pandas按行自定义列比较并设置单元格颜色,解决缩进错误
Pandas行级自定义列比较与样式缩进问题解决
一、解决"unexpected indent"缩进错误
你的链式调用代码中,第二行的.apply()前未加续行符\,导致Python解析时判定为新代码块,触发缩进错误。修正后的代码如下:
def compare_to(row, what, to, prop=''): return np.where(row.index == to, prop if row[to] > row[what] else '', '') # 修正续行符:链式调用的每一行末尾(除最后一行)都需加\ summary_FR.style.apply(compare_to, what='SPEC_TYP', to='max', prop='background-color:red', axis=1, subset=(['Chip_Cur(uAmp)'],))\ .apply(compare_to, what='SPEC_MAX', to='max', prop='background-color:red', axis=1, subset=(['Noise[uVrms]'],))\ .apply(compare_to, what='SPEC_MAX', to='max', prop='background-color:red', axis=1, subset=(['Output_Common_Mode_Voltage[V]'],))
二、实现行级自定义列比较与样式设置
针对不同行的个性化比较需求,可通过subset定位目标行,结合自定义函数实现精准样式标记,同时保留原有表格基础样式:
完整实现代码
import pandas as pd import numpy as np # 原始DataFrame拼接逻辑(保留) summary_FR = pd.concat([Chip_Cur_Summary_funct_mode2, Noise_Summary_funct_mode2, VCM_Summary_funct_mode2, Sens_Summary_funct_mode2, Vbias_Summary_funct_mode2, vcm_delta_Summary_funct_mode2, THD_FUN_M2, F_LOW_FUNC_Summary_mode2, OSC_FUNC_Summary_mode2, FOSC_FUNC_Summary_mode2, VREF_CP_FUNC_Summary_mode2, Summary_PSRR_1KHz_funct_mode2, Summary_PSRR_20Hzto20KHz_funct_mode2]) # 基础表格样式(保留) cell_hover = { 'selector': 'td:hover', 'props': [('background-color', '#ffffb3')] } index_names = { 'selector': '.index_name', 'props': 'font-style: italic; color: darkgrey; font-weight:normal;' } headers = { 'selector': 'th:not(.index_name)', 'props': 'background-color: #000066; color: white;' } # 自定义比较函数:支持灵活的条件判断 def highlight_violation(row, spec_col, target_col, condition, prop='background-color:red'): """ row: 当前行数据 spec_col: 参考的SPEC列名(如'SPEC_TYP'/'SPEC_MAX'/'SPEC_MIN') target_col: 要比较的目标列名(如'max'/'min') condition: 判断条件,传入lambda表达式,比如lambda s,t: t > s """ return np.where(row.index == target_col, prop if condition(row[spec_col], row[target_col]) else '', '') # 链式调用实现行级自定义比较 styled_table = summary_FR.style.set_table_styles([cell_hover, index_names, headers])\ # 第一行:Chip_Cur(uAmp),max超过SPEC_TYP时标红 .apply(highlight_violation, spec_col='SPEC_TYP', target_col='max', condition=lambda s,t: t > s, axis=1, subset=(['Chip_Cur(uAmp)'],))\ # 第二行:Noise[uVrms],max超过SPEC_MAX时标红 .apply(highlight_violation, spec_col='SPEC_MAX', target_col='max', condition=lambda s,t: t > s, axis=1, subset=(['Noise[uVrms]'],))\ # 第三行:Output_Common_Mode_Voltage[V],max超过SPEC_TYP时标红 .apply(highlight_violation, spec_col='SPEC_TYP', target_col='max', condition=lambda s,t: t > s, axis=1, subset=(['Output_Common_Mode_Voltage[V]'],))\ # 其他行示例:Sensitivity[V/V],min低于SPEC_MIN或max超过SPEC_MAX时标红 .apply(highlight_violation, spec_col='SPEC_MIN', target_col='min', condition=lambda s,t: t < s, axis=1, subset=(['Sensitivity[V/V]'],))\ .apply(highlight_violation, spec_col='SPEC_MAX', target_col='max', condition=lambda s,t: t > s, axis=1, subset=(['Sensitivity[V/V]'],)) # 显示样式化表格 styled_table
关键说明
- 精准定位行:通过
subset=(['行名'],)指定目标行,实现行级自定义逻辑 - 灵活条件适配:
highlight_violation函数支持传入自定义判断条件,适配不同行的比较规则(大于、小于等) - 链式调用规范:所有续行的
.apply()前必须加\,确保Python识别为同一代码块,避免缩进错误
内容的提问来源于stack exchange,提问作者Confused
相关产品推荐
相关产品推荐

