0

I would like to customize many dataframes into html (for inclusion within reports), and have been trying to achieve successive styling of specific cells of a dataframe, including value formats. More specifically, I am trying to:

a) add successive styles to the same dataframe; including general table styles, and, b) custom formats to specific cells (e.g. colors etc), AND including, c) formats to the value in specific cells (e.g. '{:.2f}')

The dataframes I use have string data in the first two rows, and in the first column. I expect this is not too pythonic, but I have a mixed-data output table requirement. So, I went forward with this approach and integrated some solutions I found from others to be helpful (Combining pandas "data frame.style" objects and outputting to html, and How to define color of specific cell in pandas dataframe based on integer position (e.g., df.iloc[1,1]) with df.style?), providing the bulk of the approach below. However, I have not found anything that provides the formatting flexibility I was after, inclusive of value formats i.e. c, above).

I have been using the following to achieve a) and b) above, which has been successful:

    # Successive styles applied.
    styled_df = (df.style
    # Style the dataframe / table using CSS/html properties/names
                .set_table_styles(
                    [{'selector': 'th',
                      'props': [('background', '#C8E8FA'),
                                ('color', 'black'),
                                ('border', '1px solid black'),
                                ('font-family', 'calibri')]},
                     {'selector': 'td',
                      'props': [('border', '1px solid black'),
                                ('font-family', 'calibri')]},
                     {'selector': 'tr:nth-of-type(odd)',
                      'props': [('background', '#f2f2f2')]},
                     {'selector': 'tr:nth-of-type(even)',
                      'props': [('background', 'white')]}, 
                     {'selector': 'table',
                      'props': [('border', '1px solid black')]},
                    ])
    # Add styling to specific cells in table, using style.apply and a defined function
                .apply(style_data_cell, axis=None)
                )
    # Show styled df in Jupyter Notebook
    styled_df

Where the defined function is:

    def style_data_cell(x):
        df1 = pd.DataFrame('', index=x.index, columns=x.columns)
        # color cells based on iloc
        color = 'background-color: lightgreen'
        df1.iloc[3:,1:] = color
        return df1

To selectively format the value-formats of specific cells, I tried with no success adding this line (and variations) to the defined function, which did not cause an error, but did not change the styled df at all:

    df1.iloc[3:,1:].style.format('{:.2f}')

Is there a way to work this into the 'styled_df' styler with the other formats? If not, is there any other approach to achieve a, b, and c, above?

0 Answers0