核心内容
Styler 内置一组 样式函数 用于快速实现高亮、渐变、条形图等效果。
一、highlight_null() — 高亮缺失值
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A': [1, np.nan, 3, 4],
'B': [np.nan, 5, 6, np.nan],
'C': [7, 8, np.nan, 10]
})
df.style.highlight_null(color='red')
# 更多选项
df.style.highlight_null(color='#ffeb3b', subset=['A', 'B'])| 参数 | 默认 | 说明 |
|---|---|---|
color | ’red’ | 背景色 |
subset | None | 限定范围 |
na_value | None | 自定义 NA 判断 |
二、highlight_min() — 高亮最小值
df.style.highlight_min(color='lightcoral')
# 按行比较最小值(axis=1)
df.style.highlight_min(color='lightcoral', axis=1)
# 限定列
df.style.highlight_min(color='lightcoral', subset=['B', 'C'])| 参数 | 默认 | 说明 |
|---|---|---|
color | ’yellow’ | 背景色 |
axis | 0 | 0=按列,1=按行 |
subset | None | 限定范围 |
props | None | 直接指定 CSS 属性列表(优先级高于 color) |
三、highlight_max() — 高亮最大值
df.style.highlight_max(color='lightgreen')
# 按行
df.style.highlight_max(color='lightgreen', axis=1)
# 自定义 props
df.style.highlight_max(props='font-weight: bold; color: darkred')四、highlight_between() — 高亮区间值
df = pd.DataFrame(np.random.randn(5, 3), columns=list('ABC'))
# 高亮 0~0.5 之间的值
df.style.highlight_between(left=0, right=0.5)
# 按列指定区间
df.style.highlight_between(
left=pd.Series([-0.5, -0.2], index=['A', 'B']),
right=pd.Series([0.5, 0.8], index=['A', 'B'])
)
# 按行 + 包含边界
df.style.highlight_between(left=0, right=1, axis=1, inclusive='both')| 参数 | 说明 |
|---|---|
left / right | 区间边界,可为标量 / Series / DataFrame |
axis | 0=按列,1=按行 |
inclusive | ’both’ / ‘neither’ / ‘left’ / ‘right’ |
color / props | 样式设置 |
五、highlight_quantile() — 高亮分位数
# 高亮前 25% 的最小值
df.style.highlight_quantile(q_left=0.0, q_right=0.25)
# 高亮后 10% 的最大值
df.style.highlight_quantile(q_left=0.9, q_right=1.0, color='orange')
# 按行计算分位数
df.style.highlight_quantile(q_left=0.0, q_right=0.1, axis=1)六、bar() — 数据条
在单元格中绘制水平条形图,直观展示数值大小。
# 基础数据条
df.style.bar()
# 指定颜色与范围
df.style.bar(color='#4CAF50', align='zero')
# 按列独立配色
df.style.bar(subset=['A'], color=['#5fba7d', '#f28e8e'])
df.style.bar(subset=['B'], color='#2196F3')
# 设置宽度与对齐
df.style.bar(width=80, align='left')| 参数 | 默认 | 说明 |
|---|---|---|
subset | None | 限定列 |
color | ’#d65f5f’ | 颜色或 [负色, 正色] |
width | 100 | 条形最大宽度(%) |
align | ’left' | 'left’ / ‘zero’ / ‘mid’ |
height | 90 | 条形高度(%) |
vmin / vmax | None | 数值范围锚点 |
# 负值从零线向两侧延伸
df.style.bar(color=['red', 'green'], align='zero')七、background_gradient() — 背景渐变
基于数值大小生成颜色渐变,类似热力图。
# 默认绿-黄-红渐变
df.style.background_gradient()
# 指定 colormap
df.style.background_gradient(cmap='Blues')
df.style.background_gradient(cmap='RdYlGn')
# 限定列 + 自定义范围
df.style.background_gradient(cmap='viridis', subset=['A', 'B'],
vmin=0, vmax=1)
# 按行独立渐变
df.style.background_gradient(cmap='Reds', axis=1)
# 低值透明,高值醒目
df.style.background_gradient(cmap=plt.cm.RdYlGn, low=0.2, high=0.9)| 参数 | 默认 | 说明 |
|---|---|---|
cmap | ’PuBu’ | matplotlib colormap |
low / high | 0 / 0 | 渐变的饱和度 / 亮度裁剪 |
axis | 0 | 渐变计算方向 |
vmin / vmax | None | 数值归一化范围 |
subset | None | 限定范围 |
text_color_threshold | 0.408 | 文字颜色切换阈值 |
八、text_gradient() — 文字渐变
# 文字颜色渐变(背景不变)
df.style.text_gradient(cmap='coolwarm')
# 限定列
df.style.text_gradient(cmap='Reds', subset=['A'])九、color_palette() — 调色板
# 使用调色板设置背景渐变
df.style.color_palette(axis=0)
# 指定名称与范围
df.style.color_palette('mako', axis=1, vmin=0, vmax=1)color_palette() 说明
pandas 2.3+ 提供,基于 seaborn / matplotlib 调色板为背景添加渐变,与
background_gradient()类似,但支持更多调色板名称。
样式函数组合示例
# 综合应用
df.style \
.background_gradient(cmap='Blues', subset=['A']) \
.bar(color='#5fba7d', subset=['B']) \
.highlight_null(color='red') \
.highlight_max(color='gold', subset=['C']) \
.format({ 'A': '{:.2f}', 'B': '{:.1%}', 'C': '{:,.0f}' })相关笔记
- 22.2 格式化方法 -
apply()/map()自定义样式 - 22.1 导出 - 将样式表导出为文件