319

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?

smci
  • 29,564
  • 18
  • 109
  • 144
Chris
  • 11,450
  • 11
  • 37
  • 62
  • 19
    `display(df)` (with `from IPython.display import display`), or `print df.to_html()` – joris Nov 11 '14 at 22:48
  • 3
    @joris, your comment seems to answer the question, so could you perhaps post it as an answer, so that the question doesn't remain unanswered? – Cristian Ciupitu Jan 11 '15 at 03:37

8 Answers8

499

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

Harmon
  • 3,839
  • 1
  • 17
  • 16
emunsing
  • 8,678
  • 3
  • 19
  • 29
  • 5
    Is it possible to ask python to automatically open browser and show `HTML(df2.to_html())` ? – Cina May 21 '16 at 10:02
  • @Cina You should be able to write the HTML to a file, and then call your favorite browser on that file, but how to do so depends a lot on the system you're on, the browser, etc. – nealmcb Nov 05 '17 at 17:33
  • 2
    HTML(df2.to_html()) does nothing. You should do display(HTML(df2.to_html())) to render the dataframe. I tried to edit your answer but somehow it was rejected. – alyaxey Dec 05 '17 at 00:10
  • 14
    on version 5.6.0 you don't need to `import display` – joel Jul 31 '18 at 11:36
  • How to deal with concatenated strings? For example to get all text from text columns. – Peter.k Feb 01 '19 at 15:11
  • Do you also know how to hide the index numbers, not for all dfs but for only the ones that I want to? – Rishabh Gupta Dec 28 '20 at 07:14
  • Audio objects dose not render it prints – user2478236 Jan 18 '22 at 06:39
61
from IPython.display import display
display(df)  # OR
print df.to_html()
nbro
  • 13,796
  • 25
  • 99
  • 185
JacobWuzHere
  • 823
  • 7
  • 11
48

This answer is based on the 2nd tip from this blog post: 28 Jupyter Notebook tips, tricks and shortcuts

You can add the following code to the top of your notebook

from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all"

This tells Jupyter to print the results for any variable or statement on it’s own line. So you can then execute a cell solely containing

df1
df2

and it will "print out the beautiful tables for both datasets".

Jonny Brooks
  • 2,714
  • 3
  • 17
  • 22
  • 4
    This solution works beautifully and solves the original problem asked. Thanks! – Zertrin Mar 08 '17 at 06:16
  • is there a way to suppress output though, if you don't want everything showing? Like ideally, putting a semicolon at the end would prevent a print. – Ion Sme Apr 06 '21 at 02:47
  • @IonSme If you put a semicolon at the end of the last line then I think it suppresses all output, otherwise I don't think it suppresses any output. If you want to suppress output of a variable then you just don't put the variable on its own line. What scenarios would you want to suppress output of some variables but still have them on their own line? – Jonny Brooks Apr 07 '21 at 10:07
19

I prefer not messing with HTML and use as much as native infrastructure as possible. You can use Output widget with Hbox or VBox:

import ipywidgets as widgets
from IPython import display
import pandas as pd
import numpy as np

# sample data
df1 = pd.DataFrame(np.random.randn(8, 3))
df2 = pd.DataFrame(np.random.randn(8, 3))

# create output widgets
widget1 = widgets.Output()
widget2 = widgets.Output()

# render in output widgets
with widget1:
    display.display(df1)
with widget2:
    display.display(df2)

# create HBox
hbox = widgets.HBox([widget1, widget2])

# render hbox
hbox

This outputs:

enter image description here

Shital Shah
  • 55,892
  • 12
  • 218
  • 175
10

In order to show the DataFrame in Jupyter Notebook just type:

   display(Name_of_the_DataFrame)

for example:

  display(df)
Hossein SLT
  • 101
  • 1
  • 2
5

It seems you can just display both dfs using a comma in between in display. I noticed this on some notebooks on github. This code is from Jake VanderPlas's notebook.

class display(object):
    """Display HTML representation of multiple objects"""
    template = """<div style="float: left; padding: 10px;">
    <p style='font-family:"Courier New", Courier, monospace'>{0}</p>{1}
    </div>"""
    def __init__(self, *args):
        self.args = args

    def _repr_html_(self):
        return '\n'.join(self.template.format(a, eval(a)._repr_html_())
                     for a in self.args)

    def __repr__(self):
        return '\n\n'.join(a + '\n' + repr(eval(a))
                       for a in self.args)

display('df', "df2")

Moondra
  • 4,059
  • 9
  • 42
  • 86
3

To display dataframes contained in a list:

dfs = [df1, df2]
display(*dfs)
Kim
  • 1,261
  • 2
  • 17
  • 23
BSalita
  • 7,625
  • 7
  • 50
  • 61
  • This displays the column/header names only, which is not what the OP asked for. Instead, they wanted the whole DataFrame to be printed in a nice format. So `display(df)` or `display(HTML(df2.to_html()))` for wider dataframes is the correct and accepted answer. – Kim May 11 '21 at 14:56
  • @Kim Please reread my answer. If you still don't believe it, verify it with code. I stand by my answer. – BSalita May 11 '21 at 15:27
  • apologies for the confusion. I did overlook the "contained in a list" part of your answer. So you are right, your example works with `dfs` being a list of dataframes. I suggest to update your code example accordingly, to make this stand out in the code as well. Especially since this is the extra value you bring to the discussion. I am more than happy to change my vote then. – Kim May 14 '21 at 10:11
  • It is common to not fully read answers. I've done that myself. Maybe as many as 40% of comments are misunderstandings. – BSalita May 14 '21 at 10:16
3

You can use markdown to create a table. You'll be asked to install tabulate package first if it is not yet available.

from IPython.display import display, Markdown

display(Markdown(df.to_markdown()))
  • This approach almost never works right for me. `HTML(df.to_html())` properly escapes characters, markdown doesn't. – jxmorris12 Feb 16 '22 at 23:59