在Jupyter Notebook中显示带有Latex列标题的DataFrame

问题描述 投票:0回答:1

我有一个DataFrame,我想用集合函数的希腊名称来显示。

df=pd.DataFrame(
      [["A",1,2],["A",3,4],["B",5,6],["B",7,8]], 
      columns=["AB","C", "N"]
)
df=df.groupby(df.AB).agg({
     "C":["count", "sum", "mean", "std"], 
     "N":["sum", "mean", "std"]
})

看起来像:

Example DataFrame

我想产生看起来像这样的东西:

enter image description here

我已经能够制作:

enter image description here

使用

import pandas as pd
import matplotlib.pyplot as plt

data = [[str(cell) for cell in row] for row in df.values]
columns = [
    r"Count", 
    r"C $\Sigma$", 
    r"C $\bar{x}$", 
    r"C $\sigma$",
    r"N $\Sigma$", 
    r"N $\bar{x}$", 
    r"N $\sigma$"]
rows = ["A", "B"]

the_table = plt.table(cellText=data,
                  rowLabels=rows,
                  colLabels=columns)

the_table.scale(4,4)
the_table.set_fontsize(24)
plt.tick_params(axis='x', which='both', bottom=False, top=False, labelbottom=False)
plt.tick_params(axis='y', which='both', right=False, left=False, labelleft=False)
for pos in ['right','top','bottom','left']:
    plt.gca().spines[pos].set_visible(False)    

df.to_latex()功能似乎可以满足我的目的,但它呈现为jupyter不支持的表格形式。

dataframe matplotlib latex jupyter multi-index
1个回答
1
投票

您可以使用Unicode字符来获取所需的字符标题,而不必担心to_latex()

如果需要边框,可以将to_htmlcustom options一起使用来格式化表格。

© www.soinside.com 2019 - 2024. All rights reserved.