gpt4 book ai didi

python - 旋转 Panda DataFrame 的列名

转载 作者:太空狗 更新时间:2023-10-29 18:30:11 24 4
gpt4 key购买 nike

我正在尝试从 pandas 制作格式良好的表格。我的一些列名称太长了。这些列的单元格很大,导致整个表格变得一团糟。

在我的示例中,是否可以在显示时旋转列名称?

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)

I would like to rotate the names of the columns as displayed here.

最佳答案

类似于:

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

可能接近你想要的:

enter image description here

see result here

关于python - 旋转 Panda DataFrame 的列名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46715736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com