KidSudi
KidSudi

Reputation: 492

Plot table and display Pandas Dataframe

I want to display my Pandas dataframe on screen in a tabular format:

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!

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.

plotting tables

EDIT 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?

Upvotes: 4

Views: 19239

Answers (1)

kiro
kiro

Reputation: 111

line 2-4 hide the graph above,but somehow the graph still preserve some space for the figure

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

Upvotes: 4

Related Questions