edp
edp

Reputation: 23

How can we color the column and row headers of pandas dataframe?

How can I show the column headings in different colors in the dataframe table that I want to save in excel format, which I previously grouped in 5?

Upvotes: 2

Views: 2843

Answers (1)

Ibrahim Berber
Ibrahim Berber

Reputation: 860

Let's say you have a dataframe in the following format.

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

And let's export it first.

data.to_excel('data.xlsx')

Now, if you already know which columns names you want to change, you can do following:

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

Now your excel file will look like this: enter image description here

You can optimize the code for further modification to be handled easily.

Upvotes: 2

Related Questions