绘制表格并显示 Pandas Dataframe
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25773991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plot table and display Pandas Dataframe
提问by KidSudi
I want to display my Pandas dataframe on screen in a tabular format:
我想以表格格式在屏幕上显示我的 Pandas 数据框:
df = pd.DataFrame({'apples': 10, 'bananas': 15, 'pears': 5}, [0])
I'm not sure how to do so. I know that pd.DataFrame.plot() has some options to display a table, but only along with the graph. I just want to display the table (i.e. dataframe) on screen. Thanks!
我不知道该怎么做。我知道 pd.DataFrame.plot() 有一些选项可以显示表格,但只能与图形一起显示。我只想在屏幕上显示表格(即数据框)。谢谢!
EDIT:
编辑:
Here's a screenshot of creating a table using pandas plot function. I only want the bottom table portion however, not the graph. I also want a popup of the table figure.
这是使用Pandas绘图功能创建表格的屏幕截图。但是,我只想要底部表格部分,而不是图表。我还想要一个表格图的弹出窗口。


EDIT 2:
编辑2:
I managed to display my dataframe on the figure with the following:
我设法在图中显示了我的数据框,如下所示:
plt.figure()
y = [0]
plt.table(cellText=[10, 15, 5], rowLabels=[0], columnLabels=['apple', 'bananas', 'pears'], loc='center')
plt.axis('off')
plt.plot(y)
plt.show()
This will display just the table without any of the axes. I don't know if this is the best way to go about it, so any suggestions would be appreciated. Also, is there a way to add a title to this table? The only way I know would be to use plt.text and place the text (title of the table) within the figure, but then I would have to keep the axes...Any ideas?
这将只显示没有任何轴的表格。我不知道这是否是最好的方法,所以任何建议将不胜感激。另外,有没有办法为此表添加标题?我知道的唯一方法是使用 plt.text 并将文本(表格的标题)放在图中,但是我必须保留轴......有什么想法吗?
回答by kiro
line 2-4 hide the graph above,but somehow the graph still preserve some space for the figure
第 2-4 行隐藏了上面的图形,但不知何故,图形仍然为图形保留了一些空间
import matplotlib.pyplot as plt
ax = plt.subplot(111, frame_on=False)
ax.xaxis.set_visible(False)
ax.yaxis.set_visible(False)
the_table = plt.table(cellText=table_vals,
colWidths = [0.5]*len(col_labels),
rowLabels=row_labels, colLabels=col_labels,
cellLoc = 'center', rowLoc = 'center')
plt.show()

