类似这样的内容:
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)'),
])]
)
可能接近您所需的内容:
rotateZ(-90deg)
参数会导致标题旋转回来,使它们再次变为水平。使用('vertical-align', 'text-top'), ('transform', 'rotateZ(180deg)')
,可以得到更好的布局,这样可以将文本沿着表格的第一行对齐。 - Nateprops=[('vertical-align', 'text-top'), ('transform', 'rotateZ(-90deg)')]
。 - Paul Rougieux查看已接受答案的解决方案的pybloqs源代码,我能够找到如何旋转列而无需安装pybloqs。请注意,这也会旋转索引,但我已添加了代码以删除它们。
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))
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))
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')
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')])])
希望这让你离目标更近了一步!
jupyter notebook
。 - BENYdf.style.set_table_styles([dict(selector="th",props=[('max-width', '50px')])])
。这是一个很好的解决方法,适用于像我例子中的短语。但我也有很长的单词。我仍然很想知道是否可能旋转!我不是CSS选择器专家,但我认为这应该可以工作,但实际上并没有:dfoo.style.set_table_styles([dict(selector="th",props=[('text-orientation', 'upright')])])
。 - Dan Fiorino