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)

      

  • 相关阅读:
    centos7环境下搭建redis及springboot连接集成
    SSL配置及练习
    centos7 环境下 搭建mysql数据库
    centos7 集群免密码登录
    IDEA如何设置spring-boot-devtools即所见即所得
    GitHub之分支创建
    联机SSH访问本地虚机环境
    2.7.3版本hadoop之HDFS环境搭建之浅谈
    Idea 打包maven项目 以及spring boot 的自动配置
    VoLTE Spec 87 Documents
  • 原文地址:https://www.cnblogs.com/bianzhiwei/p/11214994.html
Copyright © 2011-2022 走看看