4

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?

4
  • 2
    What Python GUI are you using? If it's IPython it should display nicely... Commented Sep 10, 2014 at 19:59
  • 3
    yup, even in python repl, typing df<enter> or print(df) prints nicely. Commented Sep 10, 2014 at 20:11
  • Added a screenshot of what I want to my question Commented Sep 11, 2014 at 12:44
  • By the way I'm using PyDev within Eclipse. Commented Sep 11, 2014 at 18:15

1 Answer 1

4

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.