IPython Notebook cell multiple outputs

PandasJupyter NotebookIpython

Pandas Problem Overview


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?

Pandas Solutions


Solution 1 - Pandas

have you tried the display command?

from IPython.display import display
display(salaries.head())
display(teams.head())

Solution 2 - Pandas

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.

Solution 3 - Pandas

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)

Solution 4 - Pandas

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.

Solution 5 - Pandas

Provide,

print salaries.head()
teams.head()

Solution 6 - Pandas

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLokeshView Question on Stackoverflow
Solution 1 - PandastglariaView Answer on Stackoverflow
Solution 2 - PandasAru SinghView Answer on Stackoverflow
Solution 3 - PandasMike MüllerView Answer on Stackoverflow
Solution 4 - PandasEricView Answer on Stackoverflow
Solution 5 - PandasWoodChopperView Answer on Stackoverflow
Solution 6 - PandasShreeyansh DasView Answer on Stackoverflow