IPython Notebook单元格多个输出

116

我正在IPython Notebook中运行此单元格:

# salaries and teams are Pandas dataframe
salaries.head()
teams.head()
结果是我只得到teams数据框架的输出,而没有得到salariesteams两个数据框架的输出。如果我只运行salaries.head(),那么我就可以得到salaries数据框架的结果,但是如果运行这两个语句,我只能看到teams.head()的输出。我该如何纠正这个问题?

from IPython.core.interactiveshell import InteractiveShell' InteractiveShell.ast_node_interactivity = "all" 从IPython.core.interactiveshell导入InteractiveShell' InteractiveShell.ast_node_interactivity = "all" - user7437554
6个回答

174

你尝试过使用 display 命令吗?

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

27
根据文档:“自从 IPython 5.4 和 6.1 版本以后,display() 函数已经自动提供给用户使用,无需导入库。” - Georgy
我正在使用IPython 6.4.0,我不得不使用导入语句from IPython.display import display - Gaurav Srivastava

150

更简单的方法:

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

它可以避免您不断地输入"Display"。

假设单元格包含以下内容:

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

a = 1
b = 2

a
b

那么输出结果将是:

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

如果我们使用 IPython.display.display

from IPython.display import display

a = 1
b = 2

display(a)
display(b)

输出结果为:

1
2

所以说,就是同样的事情,只是没有Out[n]这部分。


这是新的吗? 我不记得几年前看到过这个选项。 - tglaria
1
我甚至在更新的文档中都没有看到它:http://ipython.readthedocs.io/en/stable/api/generated/IPython.core.interactiveshell.html#IPython.core.interactiveshell.InteractiveShell但是它在“终端”IPython选项中:http://ipython.readthedocs.io/en/stable/config/options/terminal.html - tglaria
2
哦,我希望我能回答那个问题。我记得几个月前在另一个问题上看到过它(但我希望我能找到来源),它对我非常有效,所以我一直将其保留在我的后备库中。 - Aru Singh
3
你应该使用 get_ipython().ast_node_interactivity = 'all',而不是用一个常量字符串替换类属性! - Eric
请查看此答案,以使此配置更改在所有笔记本中通用。 - Wassadamo
显示剩余3条评论

7

枚举所有的解决方案:

在交互式会话中比较它们:

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

请注意,Jupyter 中的行为与 ipython 中完全相同。

我们如何重置它以展示原始的行为,一旦将ast_node_interactivity设置为“全部”? - Prasun Kumar Khan
1
问得太快了!!应该将其设置为get_ipython().ast_node_interactivity = 'last_expr'以恢复默认行为。其他可能的选项包括“none”、“last”和“last_expr_or_assign”。 - Prasun Kumar Khan

5

IPython Notebook只会显示一个单元格中的最后一个返回值。对于您的情况,最简单的解决方案是使用两个单元格。

如果您真的只需要一个单元格,可以像这样使用hack

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

A()

如果您经常需要此操作,请将其制作成一个函数:
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()

使用方法:

show_two_heads(salaries, teams)

超过两个人的版本:
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()

使用方法:

show_many_heads(salaries, teams, df1, df2)

2

提供:

print salaries.head()
teams.head()

5
很好,但 print salaries.head() 的输出格式不太好。 - Lokesh
请使用display()作为打印输出,因为print输出的格式可能不会很好。 - Sandhya Krishnan

1

如果您使用print函数,那么这将起作用,因为直接给出命令只会返回最后一个命令的输出。例如,

salaries.head()
teams.head()

仅适用于teams.head()的输出

同时,

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

两个命令的输出。

因此,基本上使用 print() 函数


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接