IPython Notebook cell multiple outputs

后端 未结 5 629
攒了一身酷
攒了一身酷 2020-12-22 17:34

I am running this cell in IPython Notebook:

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

The result is that I am

5条回答
  •  清歌不尽
    2020-12-22 18:19

    Enumerating all the solutions:

    • sys.displayhook(value), which IPython/jupyter hooks into. Note this behaves slightly differently from calling display, as it includes the Out[n] text. This works fine in regular python too!

    • display(value), as in this answer

    • get_ipython().ast_node_interactivity = 'all'. This is similar to but better than the approach taken by this answer.

    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.

提交回复
热议问题