【问题标题】:How can we color the column and row headers of pandas dataframe?我们如何为 pandas 数据框的列标题和行标题着色?
【发布时间】:2021-09-14 11:31:59
【问题描述】:

如何在我想以 Excel 格式保存的数据框表中以不同颜色显示列标题,我之前将其分组为 5?

【问题讨论】:

    标签: python excel pandas background-color


    【解决方案1】:

    假设您有一个以下格式的数据框。

    import pandas as pd
    import numpy as np
    
    data = pd.DataFrame(np.random.rand(5, 5), columns=[
                        'col_' + str(e) for e in range(5)], index=['col_' + str(e) for e in range(5)])
    data
    
              col_0     col_1     col_2     col_3     col_4
    col_0  0.196830  0.306508  0.515714  0.033282  0.640673
    col_1  0.278842  0.189177  0.616362  0.577048  0.805790
    col_2  0.699674  0.251704  0.146142  0.144638  0.882772
    col_3  0.794672  0.748220  0.780501  0.716122  0.278373
    col_4  0.535306  0.182256  0.662058  0.323910  0.908328
    

    让我们先导出它。

    data.to_excel('data.xlsx')
    

    现在,如果您已经知道要更改哪些列名称,您可以执行以下操作:

    group_1 = ['col_0', 'col_3', 'col_4']
    group_2 = ['col_1', 'col_2']
    
    def change_color(workbook_param, color_hex_code):
        """Returns color format for excelsheet."""
        header_format = workbook_param.add_format({
            'bold': True,
            'text_wrap': True,
            'valign': 'top',
            'fg_color': color_hex_code,
            'border': 1})
    
        return header_format
    
    
    group_1_ind = [data.columns.to_list().index(patient) for patient in group_1]
    group_1_ind  # [0, 3, 4]
    group_2_ind = [data.columns.to_list().index(patient) for patient in group_2]
    group_2_ind  # [1, 2]
    
    group_1_data = data[group_1].copy()
    group_2_data = data[group_2].copy()
    
    # Create a Pandas Excel writer.
    writer = pd.ExcelWriter('data.xlsx', engine='xlsxwriter')
    
    # Convert the dataframe to an XlsxWriter Excel object.
    data.to_excel(writer, sheet_name='Sheet1')
    
    # Get the xlsxwriter workbook and worksheet objects. You can change sheet name as well.
    workbook = writer.book
    worksheet = writer.sheets['Sheet1']
    
    # Do the modifications for desired columns.
    for col_num, value in zip(group_1_ind, group_1_data.columns.values):
        worksheet.write(0, col_num + 1, value, change_color(workbook, '#e3fc03'))
    
    for col_num, value in zip(group_2_ind, group_2_data.columns.values):
        worksheet.write(0, col_num + 1, value, change_color(workbook, '#eb4034'))
        
        
    # Do the modifications for desired indexes.
    for ind_num, value in zip(group_1_ind, group_1_data.columns.values):
        worksheet.write(ind_num + 1, 0, value, change_color(workbook, '#e3fc03'))
    
    for ind_num, value in zip(group_2_ind, group_2_data.columns.values):
        worksheet.write(ind_num + 1, 0, value, change_color(workbook, '#eb4034'))
    
    
    # Close the Pandas Excel writer and output the Excel file.
    writer.save()
    
    

    现在您的 excel 文件将如下所示:

    您可以优化代码以方便处理进一步修改。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-02
      • 1970-01-01
      • 2019-04-19
      • 2021-02-17
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 2021-08-11
      相关资源
      最近更新 更多