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 ?