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)

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM