python pandas to_excel 高级玩法

DataFrame.to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
float_format=None, columns=None, header=True, index=True,
index_label=None, startrow=0, startcol=0, engine=None,
merge_cells=True, encoding=None, inf_rep='inf', verbose=True,
freeze_panes=None):

 

1、freeze_panes :固定行和列   int类型的 tuple

2、inf_rep  : python会出现最小值  无限接近为0 的值为inf   inf_rep 为替换所有这个值

3、na_rep  :  inf_rep 为替换 np.nan 值 

4、merge_cells :默认为True   为合并索引的  

5、添加颜色 :

 

def style_apply(series, colors, **kwargs):
    """
   
    :param series: 传过来的数据是DataFrame中的一列   类型为pd.Series
    :param colors: 内容是字典  其中key 为标题名   value 为颜色
    :return:
    """
    # print(series,type(series))
    series_names = series.name
    first_name = series_names[0]
    # second_name = series_names[1] if len(series_names) > 1 else ''
    back_ground_list = list()
    for cell in series:
        back_ground = ''
        if isinstance(colors, str):
            back_ground = 'background-color: ' + colors
        elif isinstance(colors, dict):
            if first_name in colors:
                back_ground = 'background-color: ' + colors[first_name]
        else:
            logging.info('有问题')
        back_ground_list.append(back_ground)
    # print(len(back_ground_list))
    return back_ground_list
  

  

#  style_apply 为上面你的方法   colors 为颜色字典    目的是为了给不同的标题设置不同的颜色
#  DataFrame 中的的 apply 默认传参为  为 DataFrame 中的一列  Series

df = df.apply(style_apply, colors=colors, kwargs=kwargs)

  

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值