Rotate column name for Panda DataFrame

I am trying to make beautifully formatted tables from pandas. Some of my column names are too long. The cells for these columns are large, so the whole table becomes messy.

In my example, is it possible to rotate column names when they are displayed?

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.

+10
source share
5 answers

Sort of:

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

probably close to what you want:

enter image description here

see the result here

+5
source

Looking at the pybloqs source code for the answer solution I made, I was able to figure out how to rotate columns without installing pybloq. Note that this also rotates the index, but I added code to remove them.

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))

enter image description here

+2

, 90 , , text-orientation: upright, :( writing-mode, text-orientation, - . , , .

dfoo.style.set_table_styles([dict(selector="th.col_heading",props=[("writing-mode", "vertical-lr"),('text-orientation', 'upright')])])

, !

+1

Python 'pybloqs' (http://pybloqs.readthedocs.io/en/latest/), , . ( ) , 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')

Table in HTML with rotated column names

+1

I put @Bobain a good answer in a function so that I can reuse it in a laptop.

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))

enter image description here

0
source

Source: https://habr.com/ru/post/1687390/


All Articles