Show DataFrame as table in iPython Notebook

Chris picture Chris · Nov 11, 2014 · Viewed 285.8k times · Source

I am using iPython notebook. When I do this:

df

I get a beautiful table with cells. However, if i do this:

df1
df2 

it doesn't print the first beautiful table. If I try this:

print df1
print df2

It prints out the table in a different format that spills columns over and makes the output very tall.

Is there a way to force it to print out the beautiful tables for both datasets?

Answer

emunsing picture emunsing · Apr 16, 2015

You'll need to use the HTML() or display() functions from IPython's display module:

from IPython.display import display, HTML

# Assuming that dataframes df1 and df2 are already defined:
print "Dataframe 1:"
display(df1)
print "Dataframe 2:"
display(HTML(df2.to_html()))

Note that if you just print df1.to_html() you'll get the raw, unrendered HTML.

You can also import from IPython.core.display with the same effect