115

I am running this cell in IPython Notebook:

# salaries and teams are Pandas dataframe
salaries.head()
teams.head()

The result is that I am only getting the output of teams data-frame rather than of both salaries and teams. If I just run salaries.head() I get the result for salaries data-frame but on running both the statement I just see the output of teams.head(). How can I correct this?

Eric
  • 95,302
  • 53
  • 242
  • 374
Lokesh
  • 2,842
  • 7
  • 32
  • 47
  • `from IPython.core.interactiveshell import InteractiveShell' InteractiveShell.ast_node_interactivity = "all" –  May 24 '19 at 10:02

6 Answers6

172

have you tried the display command?

from IPython.display import display
display(salaries.head())
display(teams.head())
tglaria
  • 5,678
  • 2
  • 13
  • 17
147

An easier way:

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

It saves you having to repeatedly type "Display"

Say the cell contains this:

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

a = 1
b = 2

a
b

Then the output will be:

Out[1]: 1
Out[1]: 2

If we use IPython.display.display:

from IPython.display import display

a = 1
b = 2

display(a)
display(b)

The output is:

1
2

So the same thing, but without the Out[n] part.

saubhik
  • 155
  • 2
  • 6
Aru Singh
  • 1,594
  • 1
  • 9
  • 7
  • Is this new? I don't recall seeing this option a couple of years ago. – tglaria Apr 21 '17 at 14:19
  • 1
    I don't even see it in the updated documentation: http://ipython.readthedocs.io/en/stable/api/generated/IPython.core.interactiveshell.html#IPython.core.interactiveshell.InteractiveShell But is on the "Terminal" IPython options: http://ipython.readthedocs.io/en/stable/config/options/terminal.html – tglaria Apr 21 '17 at 14:40
  • 2
    Oh man, I wish I could answer that. I remember seeing it on a different question months ago (I wish I could source) and it worked perfectly for me so I've kept it in my back pocket. – Aru Singh Apr 22 '17 at 17:55
  • Would be nice adding what will this behave like, will it display for every and each line? – matanster May 25 '18 at 15:45
  • @matanster I can add that to the main answer. But it displays each expression while assigning the same "Out[x]:" to each. See answer for more clarity – Aru Singh Jul 17 '18 at 22:00
  • 3
    You should be using `get_ipython().ast_node_interactivity = 'all'`, not replacing the class property with an constant string! – Eric May 21 '20 at 17:00
  • Not surprising `from IPython.core.interactiveshell import InteractiveShell` does not work in Colab. Anybody knows how to enable multiple outputs in Colab ? – Axel Bregnsbo Jun 06 '20 at 07:13
  • See this [answer](https://stackoverflow.com/a/62736001/5289570) to make this config change common across all notebooks – Wassadamo Mar 21 '21 at 07:56
7

Enumerating all the solutions:

Comparing these in an interactive session:

In [1]: import sys

In [2]: display(1)          # appears without Out
   ...: sys.displayhook(2)  # appears with Out
   ...: 3                   # missing
   ...: 4                   # appears with Out
1
Out[2]: 2
Out[2]: 4

In [3]: get_ipython().ast_node_interactivity = 'all'

In [2]: display(1)          # appears without Out
   ...: sys.displayhook(2)  # appears with Out
   ...: 3                   # appears with Out (different to above)
   ...: 4                   # appears with Out
1
Out[4]: 2
Out[4]: 3
Out[4]: 4

Note that the behavior in Jupyter is exactly the same as it is in ipython.

Eric
  • 95,302
  • 53
  • 242
  • 374
  • How do we reset it to exhibit the original behaviour once invoked by setting ast_node_interactivity to 'all'? – Prasun Kumar Khan Oct 06 '22 at 13:50
  • 1
    Asked too quickly!! it should be set to `get_ipython().ast_node_interactivity = 'last_expr'` to revert to the default behaviour. Other possible options are 'none', 'last' and 'last_expr_or_assign'. – Prasun Kumar Khan Oct 06 '22 at 14:00
5

IPython Notebook shows only the last return value in a cell. The easiest solution for your case is to use two cells.

If you really need only one cell you could do a hack like this:

class A:
    def _repr_html_(self):
        return salaries.head()._repr_html_() + '</br>' + teams.head()._repr_html_()

A()

If you need this often, make it a function:

def show_two_heads(df1, df2, n=5):
    class A:
        def _repr_html_(self):
            return df1.head(n)._repr_html_() + '</br>' + df2.head(n)._repr_html_()
    return A()

Usage:

show_two_heads(salaries, teams)

A version for more than two heads:

def show_many_heads(*dfs, n=5):
    class A:
        def _repr_html_(self):
            return  '</br>'.join(df.head(n)._repr_html_() for df in dfs) 
    return A()

Usage:

show_many_heads(salaries, teams, df1, df2)
Mike Müller
  • 82,630
  • 20
  • 166
  • 161
2

Provide,

print salaries.head()
teams.head()
WoodChopper
  • 4,265
  • 6
  • 31
  • 55
1

This works if you use the print function since giving direct commands only returns the output of last command. For instance,

salaries.head()
teams.head()

outputs only for teams.head()

while,

print(salaries.head())
print(teams.head())

outputs for both the commands.

So, basically, use the print() function