Python 是否可以垂直显示dataframe行标题?

Python 是否可以垂直显示dataframe行标题?,python,pandas,dataframe,styles,seaborn,Python,Pandas,Dataframe,Styles,Seaborn,我有下面的代码,它显示列标题和行标题如下 styles = [ {'selector': 'th', 'props': [('background', 'grey'), ('color', 'white'),('border-style','solid'),('border-width','1px')]}, {'selector': 'td', 'props': [('color', 'grey'),('border-style','solid')

我有下面的代码,它显示列标题和行标题如下

styles = [ 
 {'selector': 'th', 
  'props': [('background', 'grey'),  
            ('color', 'white'),('border-style','solid'),('border-width','1px')]}, 
 {'selector': 'td', 
  'props': [('color', 'grey'),('border-style','solid'),('border-width','1px')]}, 
]
foo = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
bar = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
pd.crosstab(foo, bar).style.set_table_styles(styles)

是否可以像下面那样垂直显示行标题


主要问题-你能旋转文本吗

  • 需要格式化数据帧,以便第0行是多索引的一部分
  • 只需要让CSS选择器正确地识别要旋转的HTML元素
  • 其余的格式和选择器随后就可以执行了

    styles = [ 
    {'selector':'tbody>tr:first-child>th:first-child', 'props':[('transform', 'rotate(90deg)'),]},
    ]
    foo = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c'])
    bar = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f'])
    df = pd.crosstab(foo, bar)
    # need to rebuild index as row_0 needs to be part of multiindex key
    ri = pd.MultiIndex.from_tuples([tuple([df.index.name, v]) for v in df.index.values])
    df.set_index(ri).style.set_table_styles(styles)
    
    

    只需输出html并应用CSS