0

I'm using this function to change the color of a specific cells:

def cell_colours(series):
 red = 'background-color: red;'
 yellow = 'background-color: yellow;'
 green = 'background-color: green;'
 default = ''

 return [red if data == "failed" else yellow if data == "error" else green if data == "passed" 
 else default for data in series]

This only changes color of each individual cell. What I need is to change the color a the header. Is there some simple way to do this? Because when I try to use

headers = {
    'selector': 'th:not(.index_name)',
    'props': 'background-color: #000066; color: white;'
}
df = df.set_table_styles([headers])
df = df.style.apply(cell_colours)

It's giving me an error that it's not a table. I guess I need to find some method that can change only the header of the dataframe.

Thanks!

acolyter11
  • 29
  • 7

2 Answers2

1

You can use .col_heading selector

headers = {
    'selector': 'th.col_heading',
    'props': 'background-color: #000066; color: white;'
}
s = df.style.set_table_styles([headers])\
            .apply(cell_colours)
s.to_html('output.html')

enter image description here

Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52
  • Thanks, but I need to use both the .apply(cell_colours) and .set_table_styles() but it's giving me an error. – acolyter11 May 26 '22 at 10:58
  • @acolyter11 What error did you get? – Ynjxsjmh May 26 '22 at 10:59
  • If I use df = df.style.set_table_styles([headers]) and bellow that I use df = df.style.apply(cell_colours), it gives me AttributeError: 'Styler' object has no attribute 'style'. – acolyter11 May 26 '22 at 11:00
  • @acolyter11 Did you try the code I write? – Ynjxsjmh May 26 '22 at 11:01
  • Sorry, now there are no errors, but the header color is not changing.. – acolyter11 May 26 '22 at 11:04
  • 1
    @acolyter11 I attached a image how the header looks like in my side, you may provide more information. – Ynjxsjmh May 26 '22 at 11:05
  • I'm then exporting that dataframe to excel. THe cells are coloured as wanted, but the header still remains the same. I changed the values of backgroud-color to #0000FF, but it's still white.. – acolyter11 May 26 '22 at 11:09
  • I'm sorry I didn't tell you about the excel part.. Maybe that's the reason? You use html instead.. – acolyter11 May 26 '22 at 11:12
  • @acolyter11 Yes, that's the reason. CSS selector works only for HTML. – Ynjxsjmh May 26 '22 at 11:14
  • And do you know how to do it for excel? – acolyter11 May 26 '22 at 11:16
  • Because I thouht that if I change the color of the dataframe, it's used for the excel as well (as I used the function cell_colours it worked that way).. – acolyter11 May 26 '22 at 11:19
  • @acolyter11 Color for index or header uses CSS selector but for cell it doesn't. Maybe you can try the solution https://stackoverflow.com/questions/36694313/pandas-xlsxwriter-format-header. – Ynjxsjmh May 26 '22 at 11:32
0

Maybe you can try ax.table to color headers

import pandas as pd
import matplotlib.pyplot as plt

fig = plt.figure()    
ax = fig.add_subplot

tab = ax.table(cellText=df.values, colLabels=df.columns,cellLoc='left', rowLoc='left', loc='bottom', 
         colLoc = 'left', colColours =["palegreen"] * 10, bbox=[0.0,-0.45,1,.28])
AKS
  • 122
  • 5