2016-05-11 12 views
2

パンダのバージョン18.1では、デフォルトの to_html機能で行われているように、ピボットテーブルの.style要素を階層的にレンダリングする方法を理解できません。pandas pivot_tableとdataframeスタイルで階層テーブルを保存するにはどうすればよいですか?

スタイラを使用して出力をレンダリングするときに、データフレームにマルチインデックスの階層を保持するオプションはありますか?

下の2つのテーブル間の視覚的な違いを参照してください:

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> 
 
    

答えて

1

をあなた場合:

from IPython.core.display import HTML 

その後、

HTML(tbl.to_html()) 

または

HTML(tbl.style.render()) 

彼らは確かに違って見えるん。これは新しい機能であり、まだ開発中:documentation

暫定から

。機能を追加し、今後のリリースで大きな変更を加える予定です。あなたのご意見をお聞かせください。私は自分自身をやった何

私のテーブルのスタイルをto_html()の出力にidclassstyleを添付することです。

+0

私の悪い点は、 'tbl.to_html()'と 'tbl.style.render()'だったはずです。 私はそれらを電子メールに入れようとしています。 – gbronner

+0

'to_html()'の出力にどのくらい正確にクラスをつけますか?それを手動で解析することによって? – lufte

関連する問題