首页 > 解决方案 > 如何在熊猫数据框中居中标题文本?

问题描述

如何使用命令 df.head() 将 pandas 数据框中的标题文本居中?现在对齐在右边,如何改变它?我在 jupyterlab 1.0 中使用 Python 3.6

编码 :

pd.set_option('colheader_justify', 'center')

不适用于 df.head() 命令。选择 ?

标签: pythonpandasjupyter-lab

解决方案


利用:

pd.set_option('colheader_justify', 'center')

option docs

这是一个例子:

df = pd.DataFrame(np.array([np.random.randn(6),
                             np.random.randint(1, 9, 6) * .1,
                             np.zeros(6)]).T,
                   columns=['A', 'B', 'C'], dtype='float')
pd.set_option('colheader_justify', 'center')
print(df)

       A      B    C 
0 -0.600705  0.5  0.0
1 -0.195192  0.6  0.0
2 -1.227175  0.8  0.0
3  0.625125  0.7  0.0
4 -0.455949  0.3  0.0
5 -0.809112  0.7  0.0

推荐阅读