Pandas DataFrames in Jupyter: columns of equal width and centered

家住魔仙堡 提交于 2020-01-01 07:03:09

问题


Am using Jupyter Notebooks and would like to display a pandas DataFrame applying the same width to all the columns, with all values centered. What's the simplest way to achieve this, without importing libraries?

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


回答1:


New in pandas 0.17.1 is the 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])



来源:https://stackoverflow.com/questions/40990700/pandas-dataframes-in-jupyter-columns-of-equal-width-and-centered

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!