我有一个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"]
})
它看起来是这样的。
我想生成这样的东西。
我已经能够生成:
有了
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不支持的。
感谢下面的Elliot,这样的东西很好用
substitutions = {
"sum":"\u03a3",
"mean":"\u03bc",
"std":"\u03c3",
True:"\u2705",
False:"\u274c",
"count":"N",
}
pretty = df.\
rename(substitutions, axis=0).\
rename(substitutions, axis=1)
并与。
%%HTML
<style type="text/css">
table.dataframe td, table.dataframe th {
border: 1px black solid !important;
color: black !important;
}
th {
text-align: center !important;
}
</style>
可以生产
你可以使用Unicode字符来获得你想要的字符标题,而不需要费力地使用 to_latex()
.
如果你想要边框,你可以用 to_html
与 自定义选项 来格式化表格。