如何在python中最好使用pandas添加表格标题

tim*_*225 14 python dataframe pandas

我想在输出中添加一个表格标题。在rwith 中 做到这一点相对容易flextable,你只需要使用 function set_caption(),在pythonwith pandasdataframe 中是否有类似的包(也许PTable)?

这就是我现在所拥有的 图片1

这就是我想要的 图片2

请帮忙,谢谢各位!

Mak*_*aku 22

你试过这样做吗?

df.style.set_caption("Hello World")
Run Code Online (Sandbox Code Playgroud)

资料来源:熊猫造型

编辑:

如果您可以使用,这是展示表格的另一种方式 matplotlib

import matplotlib.pyplot as plt
import pandas as pd

my_frame = pd.DataFrame(data={'simulation1':[71,4.8,65,4.7],
                              'simulation2':[71,4.8,69,4.7],
                              'simulation3':[70,3.8,68,4.9],
                              'experiment':[70.3,3.5,65,4.4]})
#my_frame Display pandas table

fig = plt.figure(figsize = (8, 2))
ax = fig.add_subplot(111)

ax.table(cellText = my_frame.values,
          rowLabels = my_frame.index,
          colLabels = my_frame.columns,
          loc = "center"
         )
ax.set_title("Top 10 Fields of Research by Aggregated Funding Amount")

ax.axis("off");
Run Code Online (Sandbox Code Playgroud)


小智 22

看来这并没有将标题设置为“就地”,例如:

df.reset_index(drop=True, inplace=True)
Run Code Online (Sandbox Code Playgroud)

是相同的:

df = df.reset_index(drop=True)
Run Code Online (Sandbox Code Playgroud)

相反尝试:

df = df.style.set_caption('Top 10 Fields of Research by Aggregated Funding Amount')
Run Code Online (Sandbox Code Playgroud)


小智 8

styles = [dict(selector="caption",
                       props=[("text-align", "center"),
                              ("font-size", "150%"),
                              ("color", 'black')])]

df = df.style.set_caption("funcioasadsaaaa").set_table_styles(styles)

df
Run Code Online (Sandbox Code Playgroud)


小智 7

尝试这个

df.style.set_table_attributes("style='display:inline'").set_caption('Caption table')
Run Code Online (Sandbox Code Playgroud)

输出以及将显示的标题(以黄色高亮显示)如下所示

在此输入图像描述