• 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)

      

  • 相关阅读:
    16级第三周寒假作业F题
    16级第三周寒假作业E题
    16级第二周寒假作业J题
    16级第二周寒假作业B题
    16级第一周寒假作业D题
    16级第一周寒假作业F题
    Drupal8 社区文档之Drupal的概述
    Drupal 社区文档之一般概念
    Drupal 8 目录结构
    PhpExcel中文帮助手册|PhpExcel使用方法
  • 原文地址:https://www.cnblogs.com/bianzhiwei/p/11214994.html
Copyright © 2020-2023  润新知