备案 控制台
学习
实践
活动
专区
工具
TVP
写文章
专栏首页 张俊红 DataFrame表样式设置(二)
3 0

海报分享

DataFrame表样式设置(二)

总第138篇/张俊红

在DataFrame样式表设置的第一节 DataFrame表样式设置(一) 中我们讲了字体相关的一些设置,这一节我们讲一下,对齐方式、数字显示、条件格式相关的一些设置。

1.对齐方式

对齐方式主要有两种,一种是水平方向对齐,一种是垂直方向对齐。

1.1水平方向对齐

关于水平方向对齐情况使用的是 horizontal_alignment 参数,主要有如下几个参数值可选:

general = 'general' #一般
left = 'left' #左对齐
center = 'center' #居中对齐
right = 'right' #右对齐
fill = 'fill' #填满单元格对齐
justify = 'justify' #两端对齐
center_continuous = 'centerContinuous'
distributed = 'distributed' #分散对齐
#注意区分两端对齐和分散对齐

我们把 col_1 列设置成左对齐, col_2 列设置成右对齐, col_3 列设置成居中对齐,实现代码如下:

sf.apply_column_style(cols_to_style=["col_1"], 
                      styler_obj=Styler(horizontal_alignment="left"),
                      style_header=True)
sf.apply_column_style(cols_to_style=["col_2"], 
                      styler_obj=Styler(horizontal_alignment="right"),
                      style_header=True)
sf.apply_column_style(cols_to_style=["col_3"], 
                      styler_obj=Styler(horizontal_alignment="center"),
                      style_header=True)
ew = StyleFrame.ExcelWriter(r'my_excel.xlsx')
sf.to_excel(ew)
ew.save()

最后效果如下:

1.2垂直方向对齐

垂直方向对齐和水平方向对齐的原理一样,关于垂直对齐使用的是 vertical_alignment 参数,主要有如下几个参数值可选:

top = 'top' #靠上对齐
center = 'center' #靠中对齐
bottom = 'bottom' #靠下对齐
justify = 'justify' #两端对齐
distributed = 'distributed' #分散对齐

2.数字显示

我们知道一个数字有不同的表现形式,可以是小数点也可以是百分数,可以设置保留两位小数点还可以设置保留三位小数点。我们要想设置数字的这些显示形式,需要用到 number_format 参数,主要有如下几个参数值可选:

general = 'General' #对应Excel中的常规
general_integer = '0' #不保留小数点 
general_float = '0.00' #保留两位小数点
percent = '0.0%' #百分数
thousands_comma_sep = '#,##0' #千位分隔样式
date = 'DD/MM/YY' #年月日
time_24_hours = 'HH:MM' #小时分钟
time_24_hours_with_seconds = 'HH:MM:SS' #小时分钟秒
time_12_hours = 'h:MM AM/PM' #12小时分钟 上下午区分
time_12_hours_with_seconds = 'h:MM:SS AM/PM' #12小时分钟秒 上下午区分
date_time = 'DD/MM/YY HH:MM' #年月日时分
date_time_with_seconds = 'DD/MM/YY HH:MM:SS' #年月日时分秒

我们把 col_1 列设置成常规格式, col_2 列设置成保留3位小数点, col_3 列设置成百分数格式,实现代码如下:

sf.apply_column_style(cols_to_style=["col_1"], 
                      styler_obj=Styler(number_format="0"),
                      style_header=True)
sf.apply_column_style(cols_to_style=["col_2"], 
                      styler_obj=Styler(number_format="0.000"),
                      style_header=True)
sf.apply_column_style(cols_to_style=["col_3"], 
                      styler_obj=Styler(number_format="0.0%"),
                      style_header=True)
ew = StyleFrame.ExcelWriter(r'my_excel.xlsx')
sf.to_excel(ew)
ew.save()

最后效果如下:

3.条件格式

条件格式主要将满足条件的某些值重点突出显示出来,条件格式主要用在色阶显示中,可选的条件如下:

num = 'num' #根据具体数值
percent = 'percent' #根据百分数
max = 'max' #根据最大值
min = 'min' #根据最小值
formula = 'formula' #根据公式
percentile = 'percentile' #根据分位数

色阶调整需要用到 add_color_scale_conditional_formatting 方法。

4.行宽列高设置

4.1设置列宽

设置列宽的时候,我们可以将整个表中所有列设置成一样的宽度,也可以不同列的列宽是不一样的。设置列宽不是通过设置 Styler 来设置的,而是在 sf 表上直接调用 set_column_width set_column_width_dict 方法即可。

我们把整个表列宽都设置成10,实现代码如下:

sf.set_column_width(columns = ["col_1","col_2","col_3"],width=10)
ew = StyleFrame.ExcelWriter(r'my_excel.xlsx')
sf.to_excel(ew)
ew.save()

最后效果如下:

我们把 col_1 列列宽设置成10, col_2 列列宽设置成20, col_3 列列宽设置成30,实现代码如下:

sf.set_column_width_dict(col_width_dict = {"col_1":10,"col_2":20,"col_3":30})
ew = StyleFrame.ExcelWriter(r'my_excel.xlsx')
sf.to_excel(ew)