我有一个问题,查看以下数据框架:

n = 100
foo = DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)
foo

问题是它不会在ipython notebook中按默认值打印所有行,但我必须切片才能查看结果行。即使下面的选项也不会改变输出:

pd.set_option('display.max_rows', 500)

有人知道如何显示整个数组吗?


设置display.max_rows:

pd.set_option('display.max_rows', 500)

对于旧版本的pandas(<=0.11.0),您需要更改两个显示。Height和display.max_rows。

pd.set_option('display.height', 500)
pd.set_option('display.max_rows', 500)

请参见pd.describe_option('display')。

你可以暂时设置一个选项,就像这样:

from IPython.display import display
with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    display(df) #need display to show the dataframe when using with in jupyter
    #some pandas stuff

你也可以像这样将一个选项重置回默认值:

pd.reset_option(“display.max_rows”)

并将它们全部重置:

pd reset_option(’all’)

在这个类似问题的答案中,没有必要侵入设置。这样写要简单得多:

print(foo.to_string())

正如@hanleyhansen在评论中指出的那样,从0.18.1版本开始,显示。高度选项已弃用,并表示“使用显示”。max_rows相反”。所以你只需要这样配置它:

pd.set_option('display.max_rows', 500)

参见发行说明- pandas 0.18.1文档:

弃用。高度,显示。宽度现在只是一个格式化选项,不控制摘要的触发,类似于< 0.11.0。

就我个人而言,我喜欢直接用赋值语句设置选项,因为通过制表符补全很容易找到,这要感谢iPython。我发现很难记住确切的选项名称,所以这个方法适合我。

例如,我所要记住的就是它以pd.options开头

pd.options.<TAB>

大多数选项都显示在屏幕下方

pd.options.display.<TAB>

从这里,我通常输出当前的值是这样的:

pd.options.display.max_rows
60

然后我把它设置成我想要的样子:

pd.options.display.max_rows = 100

此外,您应该注意选项的上下文管理器,它会在代码块中临时设置选项。将选项名作为字符串传入,后面跟着您想要的值。你可以在同一行中传入任意数量的选项:

with pd.option_context('display.max_rows', 100, 'display.max_columns', 10):
    some pandas stuff

你也可以像这样将一个选项重置回默认值:

pd.reset_option('display.max_rows')

并将它们全部重置:

pd.reset_option('all')

通过pd.set_option设置选项仍然非常好。我只是发现直接使用属性更容易,不太需要get_option和set_option。

在这篇评论和回答中已经指出了这一点,但我将尝试对这个问题给出更直接的回答:

from IPython.display import display
import numpy as np
import pandas as pd

n = 100
foo = pd.DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)

with pd.option_context("display.max_rows", foo.shape[0]):
    display(foo)

熊猫。Option_context自pandas 0.13.1 (pandas 0.13.1发行说明)以来可用。 根据这个,

[it]允许[s]你用一组选项来执行一个代码块,当你退出with块时,这些选项会恢复到之前的设置。

pd.set_option('display.max_rows', 500)
df

在木星行不通! 而不是使用:

pd.set_option('display.max_rows', 500)
df.head(500)

设置无限行数使用

没有一个

也就是说,

pd.set_option('display.max_columns', None)

现在笔记本将显示笔记本内所有数据集中的所有行;)

类似地,您可以设置为显示所有列

pd.set_option('display.max_rows', None)

现在如果你使用运行单元只有数据框架,没有任何头部或尾部标签 作为

df

然后它将显示数据框架df中的所有行和列

我不知道为什么没人提这个。

你还应该设置'display.min_rows'。

pd.set_option('display.min_rows', 500)  # <-add this!
pd.set_option('display.max_rows', 500)

如果总行数> display.max_rows,

然后,设置只显示。Max_rows将不起作用。

(是的,这很令人困惑。这应该被修改。)

我将使用上下文管理器来设置这些选项,以便我可以控制哪些df应该受到影响。

with pd.option_context('display.min_rows', 50, 'max_columns', None):
    display(df)

同样,代替显示。Max_rows使用显示。min_rows代替。这应该在不设置display.max_rows的情况下工作。

pd.options.display。max_rows =无

这将显示所有的行