问题
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