In pandas version 18.1, I can't figure out how to use the .style element on a pivot table to render the result hierarchically, as is done in the default
to_html
function.
Is there some option that preserves the hierarchy of multi-indexes in dataframes when rendering output using the stylers?
See the visual differences between the two tables below:
import pandas as pd
import numpy as np
np.random.seed(0)
a = np.random.randint(1, 4, 10)
b = np.random.randint(1, 4, 10)
c = np.random.randint(5, 9, 10)
df = pd.DataFrame(dict(A=a,B=b,C=c,Val=np.random.randint(1,10,10)))
tbl=pd.pivot_table(df,index=['A','B','C'], aggfunc=sum)
print tbl.to_html()
print tbl.style.render()
Produces:
<table border="1" class="dataframe">
<thead>
<tr style="text-align: right;">
<th></th>
<th></th>
<th></th>
<th>Val</th>
</tr>
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<th rowspan="4" valign="top">1</th>
<th>1</th>
<th>5</th>
<td>1</td>
</tr>
<tr>
<th rowspan="3" valign="top">2</th>
<th>5</th>
<td>4</td>
</tr>
<tr>
<th>6</th>
<td>4</td>
</tr>
<tr>
<th>8</th>
<td>9</td>
</tr>
<tr>
<th rowspan="3" valign="top">2</th>
<th rowspan="3" valign="top">3</th>
<th>5</th>
<td>2</td>
</tr>
<tr>
<th>6</th>
<td>3</td>
</tr>
<tr>
<th>8</th>
<td>9</td>
</tr>
<tr>
<th rowspan="2" valign="top">3</th>
<th>1</th>
<th>8</th>
<td>4</td>
</tr>
<tr>
<th>2</th>
<th>7</th>
<td>4</td>
</tr>
</tbody>
</table>
<style type="text/css" >
</style>
<table id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" None>
<thead>
<tr>
<th class="blank">
<th class="blank">
<th class="blank">
<th class="col_heading level0 col0">Val
</tr>
<tr>
<th class="col_heading level2 col0">A
<th class="col_heading level2 col1">B
<th class="col_heading level2 col2">C
<th class="blank">
</tr>
</thead>
<tbody>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level2 row0">
1
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level2 row0">
1
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level2 row0">
5
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row0_col0" class="data row0 col0">
1
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row1">
1
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row1">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row1">
5
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row1_col0" class="data row1 col0">
4
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row2">
1
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row2">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row2">
6
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row2_col0" class="data row2 col0">
4
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row3">
1
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row3">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row3">
8
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row3_col0" class="data row3 col0">
9
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row4">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row4">
3
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row4">
5
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row4_col0" class="data row4 col0">
2
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row5">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row5">
3
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row5">
6
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row5_col0" class="data row5 col0">
3
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row6">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row6">
3
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row6">
8
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row6_col0" class="data row6 col0">
9
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row7">
3
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row7">
1
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row7">
8
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row7_col0" class="data row7 col0">
4
</tr>
<tr>
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row8">
3
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row8">
2
<th id="T_ae86631e_1789_11e6_aa5c_3417ebaea714" class="row_heading level0 row8">
7
<td id="T_ae86631e_1789_11e6_aa5c_3417ebaea714row8_col0" class="data row8 col0">
4
</tr>
</tbody>
</table>