Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format pandas dataframe row wise

I have the following dataframe and want to convert it to HTML

            Limit        Status     Warning      3M AVG
VAR1        1.20         1.21216    1.11         1.21235
VAR2        0.82         0.63075    0.75         0.593295
VAR3        0.38         0.376988   0.35         0.376988
VAR4        0.17         0.126987   0.14         0.12461

I want to format this dataframe row-wise such that:

  1. If Status exceeds Warning the whole row becomes highlighted yellow and if it exceeds Limit the whole row becomes highlighted red
  2. row VAR2 and VAR3 have "{:.2%}" format and VAR1 and VAR4 have "{:.2f}"

I've dug into pandas documentation and tried a couple of methods but I couldn't do all of the above tasks

I would appreciate if you could help since I believe it's a challenge for many pandas users to format a dataframe row wise.

Edit 1: I have tried the following code:

df=df.transpose()    
df.style.format("{:.2%}").format({"VAR1":"{:.2f},"VAR4":"{:.2f}"})

Note: by transposing the dataframe it is much easier to do all tasks but I cannot transpose it back to its original shape because it is styler.

like image 868
Jeff Tehrani Avatar asked Oct 12 '18 16:10

Jeff Tehrani


People also ask

How do I use conditional formatting in pandas?

Conditional cell highlighting. One way to conditionally format your Pandas DataFrame is to highlight cells which meet certain conditions. To do so, we can write a simple function and pass that function into the Styler object using . apply() or .

What is Orient in DataFrame?

Orient is short for orientation, or, a way to specify how your data is laid out. Method 1 – Orient (default): columns = If you want the keys of your dictionary to be the DataFrame column names. Method 2 – Orient: index = If the keys of your dictionary should be the index values.


1 Answers

I had the same problem and looked into implementation of the format function in pandas.io.formats.style.Styler class and implemented a similar row-wise function:

def format_row_wise(styler, formatter):
    for row, row_formatter in formatter.items():
        row_num = styler.index.get_loc(row)

        for col_num in range(len(styler.columns)):
            styler._display_funcs[(row_num, col_num)] = row_formatter
    return styler

Example:

df = pandas.DataFrame(
    {
        'Limit': [1.20, 0.82, 0.38, 0.17], 
        'Status': [1.21216, 0.63075, 0.376988, 0.126987], 
        'Warning': [1.11, 0.75, 0.35, 0.14], 
        '3M AVG': [1.21235, 0.593259, 0.376988, 0.12461]
    }, 
    index=['VAR1', 'VAR2', 'VAR3', 'VAR4']
)
formatters = {"VAR1":lambda x: f"{x:.2f}", "VAR4": lambda x: f"{x:.2f}"}
styler = format_row_wise(df.style, formatters)
styler.render()

This works for me :)

Note:

  • I only implemented the dict-formatter!
  • Format has to be given as function (here: lambda)

Hopefully, this gets you on the right way...

like image 63
Henhuy Avatar answered Sep 24 '22 11:09

Henhuy