我正试着用熊猫做一张格式很好的桌子。我的一些列名太长了。这些列的单元格太大,导致整个表混乱。
在我的示例中,是否可以在列名称显示时旋转它们?

data = [{'Way too long of a column to be reasonable':4,'Four?':4},
        {'Way too long of a column to be reasonable':5,'Four?':5}]
pd.DataFrame(data)

python - 旋转Panda DataFrame的列名称-LMLPHP

最佳答案

比如:

data = [{'Way too long of a column to be reasonable':4,'Four?':4},
        {'Way too long of a column to be reasonable':5,'Four?':5}]
dfoo = pd.DataFrame(data)
dfoo.style.set_table_styles(
    [dict(selector="th",props=[('max-width', '80px')]),
        dict(selector="th.col_heading",
                 props=[("writing-mode", "vertical-rl"),
                        ('transform', 'rotateZ(-90deg)'),
                        ])]
)

可能接近你想要的:
python - 旋转Panda DataFrame的列名称-LMLPHP
see result here

10-06 01:43