Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Pandas: Style column header

I am using pandas styler to give some columns a background color, based on the name of the column header. While this works as intended, the background color of the column header doesn't change.

Here is the part in my script where thy style is applied:

def highlight_col(x):
    if x.name in added_columns:
        return ['background-color: #67c5a4']*x.shape[0]
    elif x.name in dropped_columns:
        return ['background-color: #ff9090']*x.shape[0]
    else:
        return ['background-color: None']*x.shape[0]



old = old.style.apply(highlight_col, axis=0)

Is there a way to apply the style.apply()-function not only to the cells below the column header, but the complete column including the column header?

Edit: For clarification here is a screenshot of the excel output: screenshot of excel output

"Header 2" should have the same background color as the cells below it.

like image 515
Mr Curious Avatar asked Mar 19 '19 14:03

Mr Curious


People also ask

How do I style a Pandas Dataframe?

You can apply conditional formatting, the visual styling of a DataFrame depending on the data within, by using the DataFrame. style property. This is a property that returns a Styler object, which has useful methods for formatting and displaying DataFrames. The styling is accomplished using CSS.

How do I change the format of a column in pandas?

The best way to convert one or more columns of a DataFrame to numeric values is to use pandas. to_numeric() . This function will try to change non-numeric objects (such as strings) into integers or floating-point numbers as appropriate.

How do I change the header row in pandas?

You can replace the header with the first row of the dataframe by using df. columns = df. iloc[0]. You can use the below code snippet to replace the header with the first row of the pandas dataframe.


1 Answers

Okay, I think I figured out a way to handle formatting a column header using html 'selectors':

Using much of your code as setup:

df = pd.DataFrame('some value', columns=['Header1','Header2','Header3'], index=np.arange(12))
added_columns = 'Header2'
dropped_columns = 'Header1'

def highlight_col(x):
    if x.name in added_columns:
        return ['background-color: #67c5a4']*x.shape[0]
    elif x.name in dropped_columns:
        return ['background-color: #ff9090']*x.shape[0]
    else:
        return ['background-color: None']*x.shape[0]


col_loc_add = df.columns.get_loc(added_columns) + 2
col_loc_drop = df.columns.get_loc(dropped_columns) + 2

df.style.apply(highlight_col, axis=0)\
  .set_table_styles(
     [{'selector': f'th:nth-child({col_loc_add})',
       'props': [('background-color', '#67c5a4')]},
     {'selector': f'th:nth-child({col_loc_drop})',
       'props': [('background-color', '#ff9090')]}])

Output:

enter image description here

Note: I am using f-string which is a Python 3.6+ feature.

like image 154
Scott Boston Avatar answered Sep 22 '22 14:09

Scott Boston