在pandas版本18.1中,我不知道如何像在默认的to_html函数中那样,在数据透视表上使用.style元素分层地呈现结果。
在使用样式器呈现输出时,是否有一些选项可以保留数据帧中多索引的层次结构?
请看下面两个表之间的视觉差异:
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()产生:
<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>
发布于 2016-05-11 23:16:39
如果您:
from IPython.core.display import HTML然后
HTML(tbl.to_html())或
HTML(tbl.style.render())它们看起来确实不一样。
从documentation
临时版:这是一个新特性,还在开发中。我们将在未来的版本中添加功能,并可能做出重大改变。我们很乐意听到你的反馈。
我自己所做的就是将id、class和style附加到to_html()的输出,以设置我的表的样式。
https://stackoverflow.com/questions/37166730
复制相似问题