Python 旋转熊猫数据框的列名

Python 旋转熊猫数据框的列名,python,pandas,dataframe,jupyter-notebook,Python,Pandas,Dataframe,Jupyter Notebook,我正试着用熊猫制作格式精美的桌子。我的一些列名太长了。这些列的单元格太大,导致整个表一团糟 在我的示例中,是否可以在列名显示时旋转列名 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) 我可以将文本完全旋转90度,但我不知道如何使用文本方向:

我正试着用熊猫制作格式精美的桌子。我的一些列名太长了。这些列的单元格太大,导致整个表一团糟

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

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)

我可以将文本完全旋转90度,但我不知道如何使用
文本方向:直立
,因为它只会使文本不可见:(您缺少
写入模式
属性,必须为
文本方向
设置该属性才能产生任何效果。此外,我通过稍微修改选择器使其仅适用于列标题

dfoo.style.set_table_styles([dict(selector=“th.col_heading”,props=[(“书写模式”,“垂直lr”),(“文本方向”,“竖直”)])))))


希望这能让您更接近您的目标!

使用Python库'pybloqs'(),可以旋转列名并在顶部添加填充。唯一的缺点(如文档所述)是顶部填充不能与Jupyter内联工作。必须导出表

import pandas as pd
from pybloqs import Block
import pybloqs.block.table_formatters as tf
from IPython.core.display import display, HTML

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)

fmt_header = tf.FmtHeader(fixed_width='5cm',index_width='10%', 
                          top_padding='10cm', rotate_deg=60)
table_block = Block(dfoo, formatters=[fmt_header])

display(HTML(table_block.render_html()))
table_block.save('Table.html')

查看已接受答案的解决方案,我能够找到如何在不安装Pybloq的情况下旋转列。请注意,这也会旋转索引,但我添加了删除这些列的代码

from IPython.display import HTML, display

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}]
df = pd.DataFrame(data)

styles = [
    dict(selector="th", props=[("font-size", "125%"),
                               ("text-align", "center"),
                              ("transform", "translate(0%,-30%) rotate(-5deg)")
                              ]),
    dict(selector=".row_heading, .blank", props= [('display', 'none;')])
]

html = df.style.set_table_styles(styles).render()
display(HTML(html))

类似于:

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)'),
                        ])]
)
可能接近您想要的:


我把@Bobain的好答案放在一个函数中,这样我就可以在整个笔记本中重复使用它

def format_vertical_headers(df):
    """Display a dataframe with vertical column headers"""
    styles = [dict(selector="th", props=[('width', '40px')]),
              dict(selector="th.col_heading",
                   props=[("writing-mode", "vertical-rl"),
                          ('transform', 'rotateZ(180deg)'), 
                          ('height', '290px'),
                          ('vertical-align', 'top')])]
    return (df.fillna('').style.set_table_styles(styles))

format_vertical_headers(pandas.DataFrame(data))

< /P>你可以添加标签<代码> Juyyter笔记本< /Cord>这显然取决于你的情况,但是也许缩短或改进列名是有意义的,但是对于一个解决方案,你可以考虑转换成一个“假”多索引。例如,如果列名是“戴维的南瓜”,你可以把“戴维S”作为顶级和“南瓜”。作为第二级。显然,这不是多索引的用途,但我认为它基本上可以满足您的需要。@JohnE,谢谢您的建议。文本换行符是@Wen建议的,它被删除了:

df.style.set_table_style([dict(selector=“th”,props=[('max-width',50px')]))
这是一个很好的解决方法,适用于短语,就像在我的示例中一样。我也有很长的单词。我仍然好奇是否可以旋转!我不是CSS选择器专家,但我认为这应该有效,但没有:
dfoo.style.set_table_styles([dict(selector=“th”,props=[('text-orientation','立柱')]))
我刚刚重新阅读了文档页()这说明“您只能设置值的样式,而不能设置索引或列的样式”,但文档显示了正在格式化的列和索引。可能这才刚刚开始解决。这肯定是越来越热了!所有单个字母都被旋转,至少改变了我接受的答案。这仅使用Pandas库和wi完成任务我将在JupyterLab中显示。做得好!我发现(在Firefox中)的
rotateZ(-90度)
参数导致标题向后旋转,因此它们再次处于水平位置。我使用
('vertical-align','text top'),('transform','rotateZ(180度)')获得了更好的布局
,它将文本沿表格的第一行对齐。在@Nate的注释之后,正确旋转列名的设置是
props=[('vertical-align','text-top'),('transform','rotateZ(-90deg)]