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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 15:41:44  来源:igfitidea点击:

IPython Notebook cell multiple outputs

pandasjupyter-notebookipython

提问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 teamsdata-frame rather than of both salariesand teams. If I just run salaries.head()I get the result for salariesdata-frame but on running both the statement I just see the output of teams.head(). How can I correct this?

结果是我只得到teamsdata-frame的输出,而不是salaries和 的输出teams。如果我只是运行,salaries.head()我会得到salaries数据帧的结果,但是在运行这两个语句时,我只会看到teams.head(). 我该如何纠正?

回答by tglaria

have you tried the displaycommand?

你试过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:

列举所有解决方案:

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 中的行为完全相同。