2016-12-10 14 views
0

これは質問"Pandas DataFrames in Jupyter: columns of equal width and centered"の変形です。JupyterのPandasデータフレーム:インデックスと列を別々にフォーマットする

インデックスを除いたすべての列がインデックス化されたデータフレームを表示するにはどうすればいいですか?また、インデックスの幅やその他の属性をどのように制御できますか?

import pandas as pd 
raw_data = {'Regiment': ['Nighthawks', 'Raptors'], 
    'Company': ['1st', '2nd'], 
    'preTestScore': [4, 24], 
    'postTestScore': [25, 94]} 
df = pd.DataFrame(raw_data, columns = ['Regiment', 'Company', 'preTestScore', 'postTestScore']).set_index('Regiment') 
d = dict(selector="th", 
    props=[('text-align', 'center')]) 

df.style.set_properties(**{'width':'10em', 'text-align':'center'})\ 
     .set_table_styles([d]) 

enter image description here

答えて

1

ただ、次のスタイルを追加します。

style_index = dict(selector=".row_heading", props=[("text-align", "right")])

+0

ありがとうございました。私はあなたがset_table_stylesに複数のスタイルを持つことができないことを知りませんでした。 – CarlosE

0
import pandas as pd 
raw_data = {'Regiment': ['Nighthawks', 'Raptors'], 
    'Company': ['1st', '2nd'], 
    'preTestScore': [4, 24], 
    'postTestScore': [25, 94]} 
df = pd.DataFrame(raw_data, columns = ['Regiment', 'Company', 'preTestScore', 'postTestScore']).set_index('Regiment') 
d1 = dict(selector="th", props=[('text-align', 'center')]) 
d2 = dict(selector=".row_heading", props=[("text-align", "left")]) 

df.style.set_properties(**{'width':'10em', 'text-align':'center'})\ 
     .set_table_styles([d1,d2]) 

Solution

関連する問題