zoukankan      html  css  js  c++  java
  • 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)

      

  • 相关阅读:
    Mysql语句练习
    Mysql-------查询各科成绩前三名的记录
    Mysql--查询"01"课程比"02"课程成绩高的学生的信息及课程分数
    模态框拖拽案例分析--元素偏移量 offset 系列
    CSS中z-index的属性与使用
    《将博客搬至CSDN》
    CSS中Position几种属性的总结
    考研数学一
    ubuntu16.04安装mysql报错解决
    LoRaWAN 规范1.0 (章节10~13)
  • 原文地址:https://www.cnblogs.com/bianzhiwei/p/11214994.html
Copyright © 2011-2022 走看看