Jupyter中的Pandas DataFrames:宽度相等且居中的列

时间:2016-12-06 08:16:14

标签: python pandas jupyter-notebook

使用Jupyter笔记本,并希望显示一个pandas DataFrame,对所有列应用相同的宽度,所有值都居中。在不导入库的情况下,实现这一目标的最简单方法是什么?

import pandas as pd
raw_data = {'regiment': ['Nighthawks', 'Dragoons'], 
    'company': ['1st', '2nd'], 
    'name': ['Miller', 'Jacob'], 
    'preTestScore': [4, 24],
    'postTestScore': [25, 94]}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'name', 'preTestScore', 'postTestScore'])
df

Unaligned columns

1 个答案:

答案 0 :(得分:5)

pandas 0.17.1中的新内容是pd.Styler

raw_data= {'regiment': ['Nighthwawks','Dragons'],
            'company':['1st','2nd'],
            'name':['Miller', 'Ali'],
            'preTestScore':[4,120]}
df = pd.DataFrame(raw_data)

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