How to Edit Header Row in Pandas - Styling

How to Edit Header Row in Pandas - Styling

This is Cascading Style Sheets (CSS)

Links

  1. https://www.w3schools.com/cssref/
  2. https://cssreference.io/
  3. https://developer.mozilla.org/en-US/docs/Web/CSS/Reference

You want 'color' not 'font color' (IIUC)

    df = pd.DataFrame(1, [*'abc'], [*'xyz'])

df.style.set_table_styles(
[{
'selector': 'th',
'props': [
('background-color', 'black'),
('color', 'cyan')]
}])

Sample Image

How can I change the styles of pandas DataFrame headers?

I've found out I need to pass a parameter as a list to Styler.set_table_styles(). It's now working with the following code.

html = (df.style
.set_table_styles([{'selector': 'th', 'props': [('font-size', '5pt')]}])
.set_properties(**{'font-size': '10pt'}).render())

Python/Pandas style column headers

Here's an answer based on the documentation for xlsxwriter:

Start by creating an xlsxwriter object and writing the dataframe to it. header=False means that we don't write the column names and startrow=1 leaves a blank row at the top (where we can put our custom columns next). Then we get the relevant objects for the workbook and worksheet.

writer = pd.ExcelWriter("output.xlsx", engine='xlsxwriter')
df.to_excel(writer, sheet_name='Sheet1', startrow=1, header=False)
workbook = writer.book
worksheet = writer.sheets['Sheet1']

We create a header format:

header_format = workbook.add_format({
'bold': True,
'text_wrap': True,
'valign': 'top',
'fg_color': '#D7E4BC',
'border': 1})

Let's say you have three columns, A, B, and C, but only want to highlight A and C. We make a list of the column names we want to highlight and apply the formatting to them selectively:

columns_to_highlight = ['A', 'C']
for col_num, col_name in enumerate(df.columns.values):
if col_name in columns_to_highlight:
worksheet.write(0, col_num + 1, col_name, header_format)
else:
worksheet.write(0, col_num + 1, col_name)
writer.save()

Change Color of Header Row

Use backgroundColor instead background-color. The first is for javascript, DOM manipulation. The second one is for CSS style declaration.

How can i make specific styling according to my column name (pandas)

From version 1.3.0, Pandas applymap accepts a subset parameter:

subset : label, array-like, IndexSlice, optional

A valid 2d input to DataFrame.loc[], or, in the case of a 1d input
or single key, to DataFrame.loc[:, ] where the columns are prioritised, to limit data to before applying the function.

So, in order, for instance, to colorize "Col1" only in the Excel output file, you can modify your code like this:

styler.applymap(_color_red_or_green, subset=["Col1"])

From there, you could define the following function:

def colorize(df, cols):
def _color_red_or_green(val):
color = "red" if val < 0 else "green"
return "color: %s" % color

styler = df.style

styler.applymap(_color_red_or_green, subset=cols)

styler.to_excel("Output.xlsx")

And then call it with the dataframe and the columns of your choice:

colorize(df, ["Col1", "col3"])

Which outputs an Excel file with both "Col1" en "col3" values painted as green.



Related Topics



Leave a reply



Submit