pandas IPython Notebook cell 多路输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34398054/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IPython Notebook cell multiple outputs
提问by Lokesh
I am running this cell in IPython Notebook:
我在 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?
结果是我只得到teams
data-frame的输出,而不是salaries
和 的输出teams
。如果我只是运行,salaries.head()
我会得到salaries
数据帧的结果,但是在运行这两个语句时,我只会看到teams.head()
. 我该如何纠正?
回答by tglaria
have you tried the display
command?
你试过display
命令吗?
from IPython.display import display
display(salaries.head())
display(teams.head())
回答by Aru Singh
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
:
如果我们使用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.
所以同样的事情,但没有Out[n]
部分。
回答by Mike Müller
IPython Notebook shows only the last return value in a cell. The easiest solution for your case is to use two cells.
IPython Notebook 仅显示单元格中的最后一个返回值。对于您的情况,最简单的解决方案是使用两个单元格。
If you really need only one cell you could do a hacklike 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)
回答by WoodChopper
Provide,
提供,
print salaries.head()
teams.head()
回答by Eric
Enumerating all the solutions:
列举所有解决方案:
sys.displayhook(value)
, which IPython/jupyter hooks into. Note this behaves slightly differently from callingdisplay
, as it includes theOut[n]
text. This works fine in regular python too!display(value)
, as in this answerget_ipython().ast_node_interactivity = 'all'
. This is similar to but better than the approach taken by this answer.
sys.displayhook(value)
,其中 IPython/jupyter 挂钩。请注意,这与调用 的行为略有不同display
,因为它包含Out[n]
文本。这在普通 python 中也很好用!get_ipython().ast_node_interactivity = 'all'
. 这类似于但优于此答案采用的方法。
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.
请注意,Jupyter 中的行为与 ipython 中的行为完全相同。