How to correctly use the styling of pandas?

3

Python code

From the examples from documentation the following code was 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 italicized, with the function df.style.highlight_max(axis=0) or custom function df.style.apply(highlight_max)

But the function returns the following CSS / HTML style:

<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 df.style.render() function 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 properly use Pandas ?

    
asked by anonymous 18.12.2018 / 20:04

1 answer

0

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

Then with:

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

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

  

AttributeError: 'Styler' object has no attribute 'style'

The correct thing would be to pass all object attributes, methods, and properties 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()
    
20.12.2018 / 12:05