Jupyter 中的表格样式 高亮设置

1、数据及环境准备

pandas版本需满足1.3.0以上,否则部分功能无法实现

import pandas as pd
import numpy as np

path = r"C:\Users\a\Desktop\测试数据.xlsx"
data = pd.read_excel(path).iloc[:10,:20]

print(f'pandas version:{pd.__version__}')
print(f'numpy version:{np.__version__}')

data

在这里插入图片描述
在这里插入图片描述

2、隐藏索引 style.hide_index()

data.style.hide_index()

在这里插入图片描述

3、隐藏列style.hide_columns()

data.style.hide_index().hide_columns(["机构","ID"])

在这里插入图片描述

4、设置数据格式 style.format()

4.1指定格式化format(format_dict)

可以用 DataFrame.dtypes 属性来查看数据格式。

  • 对于字符串类型,一般不要进行格式设置;

  • 对于数字类型,是格式设置用的最多的,包括设置小数的位数、千分位、百分数形式、金额类型等;

  • 对于时间类型,经常会需要转换为字符串类型进行显示;

  • 对于空值,可以通过 na_rep 参数来设置显示内容;

format_dict = {'属性4': '¥{0:.1f}', 
               '属性5': '{0:.2f}', 
               '属性6': '{0:.3f}', 
               '测试日期':lambda x: "{}".format(x.strftime('%Y%m%d')),
                }
                
data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict)

在这里插入图片描述

4.2 空值填充format(None,na_rep='--')

注:format(format_dict)与format(None,na_rep=‘–’)可以分开也可以合并为format(format_dict,na_rep=‘–’)

data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict,na_rep='--')

在这里插入图片描述

5、颜色高亮设置

个性化设置
highlight_max()、highlight_min()、highlight_null() 等函数可以通过 props 参数来进行颜色、字体等修改。

  • 字体颜色和背景颜色设置props='color:black;background-color:#ee7621'
  • 字体加粗以及字体颜色设置props='font-weight:bold;color:#ee7621'

字体加粗以及字体颜色设置

5.1 最大值高亮显示highlight_max()

data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict)\
.format(None,na_rep='--')\
.highlight_max("年龄",color = "green",axis=0)
#axis=0 是按列进行统计

在这里插入图片描述

5.2 最小值高亮显示 highlight_min()

data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict)\
.format(None,na_rep='--')\
.highlight_max("年龄",color = "green",axis=0)\
.highlight_min(subset = ["属性3","属性4","属性5"],axis = 1)
#axis=1 是按行进行统计

在这里插入图片描述

5.3空值高亮显示 highlight_null()

data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict,na_rep='--')\
.highlight_max("年龄",axis=0,props="color:black;background-color:#99ff66")\
.highlight_min(subset = ["属性3","属性4","属性5"],axis = 1,props='color:black;background-color:#ee7621')\
.highlight_null(props='color:white;background-color:darkblue')

在这里插入图片描述

5.4区间数据高亮显示 highlight_between()

data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict,na_rep='--')\
.highlight_max("年龄",axis=0,props="color:black;background-color:#99ff66")\
.highlight_min(subset = ["属性3","属性4","属性5"],axis = 1,props='color:black;background-color:#ee7621')\
.highlight_null(props='color:white;background-color:darkblue')\
.highlight_between(left=10,right=30,subset=["属性3","属性4","属性5"],props='font-weight:bold;color:#ee7621')

在这里插入图片描述

6、色阶颜色设置

6.1 背景色阶颜色设置 background_gradient()

参数Styler.background_gradient(cmap='PuBu', low=0, high=0, axis=0, subset=None, text_color_threshold=0.408, vmin=None, vmax=None, gmap=None)

data.style.hide_index()\
.background_gradient(cmap='Blues')

在这里插入图片描述

如果不对 subset 进行设置,background_gradient 函数将默认对所有数值类型的列进行背景颜色标注

对 subset 进行设置后,可以选择特定的列或特定的范围进行背景颜色的设置。

颜色的深度由数值的大小决定

data.style.hide_index()\
.background_gradient(subset = ["属性6"],cmap='Blues')

在这里插入图片描述

可以通过对 low 和 high 值的设置,可以来调节背景颜色的范围,low 和 high 分别是压缩 低端和高端的颜色范围,其数值范围一般是 0~1

data.style.hide_index()\
.background_gradient(subset = ["属性6"],cmap='Blues',low=0.5,high=1)

在这里插入图片描述

当数据范围比较大时,可以通过设置 vmin 和 vmax 来设置最小和最大的颜色的设置起始点。

比如下面,基金规模在30以下的,颜色最浅,规模50以上的,颜色最深,30~50亿之间的,颜色渐变

data.style.hide_index()\
.background_gradient(subset = ["属性6"],cmap='Blues',low=0.5,high=1,vmin=30,vmax=50)

在这里插入图片描述

通过 gmap 的设置,可以方便的按照某列的值,对行进行全部的背景设置

data.style.hide_index()\
.background_gradient(cmap='Blues',gmap=data['属性6'])

在这里插入图片描述

6.2文本色阶颜色设置text_gradient()

参数Styler.text_gradient(cmap='PuBu', low=0, high=0, axis=0, subset=None, vmin=None, vmax=None, gmap=None)

text_gradient() 函数的用法跟 background_gradient() 函数的用法基本是一样的

data.style.hide_index()\
.text_gradient(cmap='RdYlGn')

在这里插入图片描述

data.style.hide_index()\
.text_gradient(cmap='RdYlGn',gmap = data["属性4"])

在这里插入图片描述

7、数据条显示 DataFrame.style.bar()

参数 Styler.bar(subset=None, axis=0, color='#d65f5f', width=100, align='left', vmin=None, vmax=None)

设置对齐方式(可以通过设置 aligh 参数的值来控制显示方式)

  • left: 最小值从单元格的左侧开始。
  • zero: 零值位于单元格的中心。
  • mid: 单元格的中心在(max-min)/ 2,或者如果值全为负(正),则零对齐于单元格的右(左)。

关于颜色设置,color=[‘#99ff66’,‘#ee7621’], color可以设置为单个颜色,所有的数据只显示同一个颜色,也可以设置为包含两个元素的list或tuple形式,左边的颜色标注负数值,右边的颜色标注正数值

data.style.hide_index()\
.bar(subset=['属性7','属性8','属性3'],color=['#99ff66','#ee7621'],align='mid')

在这里插入图片描述

8、自定义函数的使用

通过 apply 和 applymap 函数,用户可以使用自定义函数来进行样式设置

  • apply 通过axis参数,每一次将一列或一行或整个表传递到DataFrame中。对于按列使用 axis=0, 按行使用 axis=1, 整个表使用 axis=None。
  • applymap 作用于范围内的每个元素。

先自定义了函数max_value(),用来找到符合条件的最大值,apply 使用的示例代码如下:

8.1 apply

8.1.1 axis=0 按列设置样式

def max_value(x, color='red'):
    return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};background-color:yellow", None)
    
data.style.hide_index()\
.apply(max_value,axis=0,subset=['属性7','属性8','属性4'])  

在这里插入图片描述

8.1.2 axis=1 按行设置样式

def max_value(x, color='red'):
    return np.where(x == np.nanmax(x.to_numpy()), f"color: {color};background-color:yellow", None)

data.style.hide_index()\
.apply(max_value,axis=1,subset=['属性7','属性8','属性4'])

在这里插入图片描述

8.2 applymap

def color_returns(val):
    if val >=0:
        color = '#EE7621'  # light red
    elif val <0:
        color =  '#99ff66' # light green  '#99ff66'
    else:
        color = '#FFFAFA'  # ligth gray
    return f'background-color: {color}'

data.style.hide_index()\
.applymap(color_returns,subset=["属性3","属性4","属性5"])

在这里插入图片描述

9、数据范围选择

在使用 Style 中的函数对表格数据进行样式设置时,对于有 subset 参数的函数,可以通过设置 行和列的范围来控制需要进行样式设置的区域

9.1 对行(row)进行范围设置

必须全都是数字数据,否则会报错

def color_returns(val):
    if val >=0:
        color = '#EE7621'  # light red
    elif val <0:
        color =  '#99ff66' # light green  '#99ff66'
    else:
        color = '#FFFAFA'  # ligth gray
    return f'background-color: {color}'

data[["属性1","属性2","属性3","属性4"]].style.hide_index()\
.applymap(color_returns,subset=pd.IndexSlice[3:5,])

在这里插入图片描述

9.2 对列(column)进行范围设置

def color_returns(val):
    if val >=0:
        color = '#EE7621'  # light red
    elif val <0:
        color =  '#99ff66' # light green  '#99ff66'
    else:
        color = '#FFFAFA'  # ligth gray
    return f'background-color: {color}'

data[["属性1","属性2","属性3","属性4"]].style.hide_index()\
.applymap(color_returns,subset=["属性3","属性4"])

在这里插入图片描述

9.3 行列双选

def color_returns(val):
    if val >=0:
        color = '#EE7621'  # light red
    elif val <0:
        color =  '#99ff66' # light green  '#99ff66'
    else:
        color = '#FFFAFA'  # ligth gray
    return f'background-color: {color}'

data[["属性1","属性2","属性3","属性4"]].style.hide_index()\
.applymap(color_returns,subset=pd.IndexSlice[2:7,["属性1","属性3"]])

在这里插入图片描述

10、共享样式(同excel保存模板)

  • Styler.export() 导出
  • Styler.use() 引用
my_style = data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict,na_rep='--')\
.highlight_max("年龄",axis=0,props="color:black;background-color:#99ff66")\
.highlight_min(subset = ["属性3","属性4","属性5"],axis = 1,props='color:black;background-color:#ee7621')\
.highlight_null(props='color:white;background-color:darkblue')

data.style.use(my_style.export())

在这里插入图片描述

11、导出样式到Excel

DataFrames 使用 OpenPyXL 或XlsxWriter 引擎可以将样式导出到 Excel 工作表。

my_style = data.style.hide_index()\
.hide_columns(["机构","ID"])\
.format(format_dict,na_rep='--')\
.highlight_max("年龄",axis=0,props="color:black;background-color:#99ff66")\
.highlight_min(subset = ["属性3","属性4","属性5"],axis = 1,props='color:black;background-color:#ee7621')\
.highlight_null(props='color:white;background-color:darkblue')\
.to_excel(r'C:\Users\a\Desktop\style_export.xlsx',engine = 'openpyxl')

在这里插入图片描述

跟共享样式里有些相同的问题,比如隐藏索引、隐藏列、设置数据格式等效果并没有实现

参考链接

  • 4
    点赞
  • 31
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

YouShouldKnowMe

别来这套

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值