Jupyter中的Pandas DataFrames:等宽且居中的列 [英] Pandas DataFrames in Jupyter: columns of equal width and centered

查看:842
本文介绍了Jupyter中的Pandas DataFrames:等宽且居中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jupyter Notebooks,并且希望显示一个对所有列都应用相同宽度且所有值都居中的pandas DataFrame.在不导入库的情况下最简单的方法是什么?

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

推荐答案

pandas 0.17.1中的新功能是 pd.Styler :

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])

这篇关于Jupyter中的Pandas DataFrames:等宽且居中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆