绘图表并显示Pandas Dataframe

时间:2014-09-10 19:56:01

标签: python pandas dataframe

我想以表格格式在屏幕上显示我的Pandas数据框:

df = pd.DataFrame({'apples': 10, 'bananas': 15, 'pears': 5}, [0])

我不知道该怎么做。我知道pd.DataFrame.plot()有一些显示表的选项,但只与图表一起显示。我只想在屏幕上显示表格(即数据帧)。谢谢!

修改

这是使用pandas plot函数创建表格的屏幕截图。我只想要底部表部分,而不是图表。我还想要一个表格的弹出窗口。

plotting tables

编辑2:

我设法在图上显示我的数据框,其中包含以下内容:

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()

这将只显示没有任何轴的表格。我不知道这是否是最佳方式,所以任何建议都将不胜感激。另外,有没有办法为这个表添加标题?我知道的唯一方法是使用plt.text并将文本(表格的标题)放在图中,但是我必须保留轴...任何想法?

1 个答案:

答案 0 :(得分:4)

第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()