How to use pandas styling properly?

Asked

Viewed 166 times

2

Python code

From the examples of documentation the following code has been created:

import pandas as pd
import os
import webbrowser
import io

def highlight_max(s):
    '''
    highlight the maximum in a Series yellow.
    '''
    is_max = s == s.max()
    return ['background-color: yellow' if v else '' for v in is_max]

df = pd.DataFrame([[1,2,3], [5,2,4], [3,8,6]], columns=list("ABC"))

# df.style.apply(highlight_max)
df.style.highlight_max(axis=0)
df_html = df.style.render()

# df_html = df.to_html()
print(df_html)


path = os.path.abspath('temp.html')
url = 'file://' + path

with io.open(path, "w", encoding="utf-8") as f:
    f.write(df_html)

webbrowser.open(url)

Style

Where the maximum value of each column should be underlined, with the function df.style.highlight_max(axis=0) or custom function df.style.apply(highlight_max)

But the function returns the following style CSS/HTML (blank):

<style  type="text/css" >
</style>  
<table id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0" > 
<thead>    <tr> 
        <th class="blank level0" ></th> 
        <th class="col_heading level0 col0" >A</th> 
        <th class="col_heading level0 col1" >B</th> 
        <th class="col_heading level0 col2" >C</th> 
    </tr></thead> 
<tbody>    <tr> 
        <th id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0level0_row0" class="row_heading level0 row0" >0</th> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row0_col0" class="data row0 col0" >1</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row0_col1" class="data row0 col1" >2</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row0_col2" class="data row0 col2" >3</td> 
    </tr>    <tr> 
        <th id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0level0_row1" class="row_heading level0 row1" >1</th> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col0" class="data row1 col0" >5</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col1" class="data row1 col1" >2</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col2" class="data row1 col2" >4</td> 
    </tr>    <tr> 
        <th id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0level0_row2" class="row_heading level0 row2" >2</th> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col0" class="data row2 col0" >3</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col1" class="data row2 col1" >8</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col2" class="data row2 col2" >6</td> 
    </tr></tbody> 
</table> 

But in the documentation, the function df.style.render() should return the following CSS/HTML:

<style  type="text/css" >
    #T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col0 {
             background-color:  yellow;
         }
    #T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col1 {
             background-color:  yellow;
         }
    #T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col2 {
             background-color:  yellow;
         }
         </style>
<table id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0" > 
<thead>    <tr> 
        <th class="blank level0" ></th> 
        <th class="col_heading level0 col0" >A</th> 
        <th class="col_heading level0 col1" >B</th> 
        <th class="col_heading level0 col2" >C</th> 
    </tr></thead> 
<tbody>    <tr> 
        <th id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0level0_row0" class="row_heading level0 row0" >0</th> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row0_col0" class="data row0 col0" >1</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row0_col1" class="data row0 col1" >2</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row0_col2" class="data row0 col2" >3</td> 
    </tr>    <tr> 
        <th id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0level0_row1" class="row_heading level0 row1" >1</th> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col0" class="data row1 col0" >5</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col1" class="data row1 col1" >2</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row1_col2" class="data row1 col2" >4</td> 
    </tr>    <tr> 
        <th id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0level0_row2" class="row_heading level0 row2" >2</th> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col0" class="data row2 col0" >3</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col1" class="data row2 col1" >8</td> 
        <td id="T_cdda8a98_02f5_11e9_82ee_40b89ae87fc0row2_col2" class="data row2 col2" >6</td> 
    </tr></tbody> 
</table> 

Problem

How to use the Styling of Pandas?

  • Solved, wrong syntax... df_html = df.style.highlight_max(axis=0).render() But I will leave the question open if someone gets other ways or a better way to accomplish this.

  • 1

    You’d better answer your own question. She’s good. :-)

  • Read: https://answall.com/help/self-answer

1 answer

0


The syntax df_html = df.style.render() is wrong if df is not a dataframe, as the input parameter of Styler is a dataframe.

So with:

df = df.style.highlight_max(axis=0)
df_html = df.style.render()

The input parameter is an object Styler, generating the following error message:

Attributeerror: 'Styler' Object has no attribute 'style'

The correct would be to pass all attributes, methods and properties of the object at once as follows:

def hover(hover_color="#ffff99"):
    return dict(selector="tr:hover",
                props=[("background-color", "%s" % hover_color)])

custom_styles = [
    hover(),
    dict(selector="th", props=[("font-size", "150%"),
                               ("text-align", "center")]),
    dict(selector="caption", props=[("caption-side", "bottom")])
]
df_html = df.style.set_properties(**{'max-width': '500px', 'font-size': '10pt'}) \
    .highlight_null(null_color='red') \
    .applymap(highlight) \
    .set_table_styles(custom_styles) \
    .render()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.