0

I want to set background color of specific headers in dataframe

Here is the Original Dataframe:

1

and here is what i want:

1

I have tried this code so far, but it doesn't work:

df1.style.set_table_styles(
       [{
           'selector': 'th',
           'props': [('background-color', 'red')]
       }])
KazikM
  • 1,257
  • 5
  • 21
  • 29
  • Does this answer your question? [Python Pandas: Style column header](https://stackoverflow.com/questions/55243717/python-pandas-style-column-header) – Paul Brennan Jan 05 '21 at 11:29

1 Answers1

0

This should help you!

import xlwings as xw
df1.to_excel("file.xlsx",index=False,header=False)
wb = xw.Book("file.xlsx")

# color is set with an rgb value 
wb.sheets['Sheet 1'].range('A').color = (169,169,169)

This basically writes to excel ad then changes the format as needed. You can change the color to any RGB values you would want to.

piet.t
  • 11,718
  • 21
  • 43
  • 52
srishtigarg
  • 1,106
  • 10
  • 24