Pandas DataFrame样式调整:添加统一边框、居中表头并统一行背景色
Pandas DataFrame样式优化:统一行背景色+表头居中+边框设置
你遇到的行背景色不一致是Pandas样式默认的**斑马纹(奇偶行区分)**效果,只需添加样式覆盖默认设置即可,同时可以直接在样式配置里实现表头居中。
修改后的代码如下:
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, Output_Impedance_FUNC_Summary_mode2, Output_Impedance_Freq_FUNC_Summary_mode2]) # 配置样式:边框+表头居中+统一行背景色 summary_FR.style.set_table_styles( [ # 表格整体边框 {"selector": "", "props": [("border", "1px solid grey")]}, # 单元格边框 {"selector": "tbody td", "props": [("border", "1px solid grey")]}, # 表头样式:边框+居中对齐 {"selector": "th", "props": [("border", "1px solid grey"), ("text-align", "center")]}, # 统一所有行的背景色(替换你需要的颜色,比如白色#ffffff) {"selector": "tbody tr", "props": [("background-color", "#ffffff")]} ] )
关键改动说明:
- 添加
{"selector": "th", "props": [("text-align", "center")]}实现表头居中对齐 - 添加
{"selector": "tbody tr", "props": [("background-color", "#ffffff")]}覆盖默认斑马纹,设置统一的行背景色(可根据需求替换颜色值,比如浅灰色#f5f5f5) - 如果需要给表头单独设置特殊背景色,可追加配置:
{"selector": "thead th", "props": [("background-color", "#e0e0e0")]}
内容的提问来源于stack exchange,提问作者Confused
相关产品推荐
相关产品推荐

