Python 熊猫:设置没有。最大行数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16424493/
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-08-18 22:35:31  来源:igfitidea点击:

Pandas: Setting no. of max rows

pythonformattingpandasipython-notebook

提问by Andy

I have a problem viewing the following DataFrame:

我在查看以下内容时遇到问题DataFrame

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

The problem is that it does not print all rows per default in ipython notebook, but I have to slice to view the resulting rows. Even the following option does not change the output:

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

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

Does anyone know how to display the whole array?

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

采纳答案by Wouter Overmeire

Set display.max_rows:

设置display.max_rows

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

For older versions of pandas (<=0.11.0) you need to change both display.heightand display.max_rows.

对于旧版本的熊猫 (<=0.11.0),您需要同时更改display.heightdisplay.max_rows

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

See also pd.describe_option('display').

另见pd.describe_option('display')

You can set an option only temporarilyfor this one time like this:

您只能暂时为这一次设置一个选项,如下所示:

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

You can also reset an option back to its default value like this:

您还可以将选项重置为其默认值,如下所示:

pd.reset_option('display.max_rows')

pd.reset_option('display.max_rows')

And reset all of them back:

并将它们全部重置:

pd.reset_option('all')

pd.reset_option('all')

回答by Ninjakannon

As in this answerto a similar question, there is no need to hack settings. It is much simpler to write:

正如在对类似问题的回答一样,无需修改设置。写起来要简单得多:

print(foo.to_string())

回答by nealmcb

As @hanleyhansen noted in a comment, as of version 0.18.1, the display.heightoption is deprecated, and says "use display.max_rowsinstead". So you just have to configure it like this:

正如@hanleyhansen 在评论中指出的那样,从 0.18.1 版开始,该display.height选项已被弃用,并表示“display.max_rows改为使用”。所以你只需要像这样配置它:

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

See the Release Notes — pandas 0.18.1 documentation:

请参阅发行说明 — pandas 0.18.1 文档

Deprecated display.height, display.width is now only a formatting option does not control triggering of summary, similar to < 0.11.0.

弃用 display.height,display.width 现在只是一个格式化选项,不控制摘要的触发,类似于 < 0.11.0。

回答by Ted Petrou

Personally, I like setting the options directly with an assignment statement as it is easy to find via tab completion thanks to iPython. I find it hard to remember what the exact option names are, so this method works for me.

就我个人而言,我喜欢直接使用赋值语句设置选项,因为有了 iPython,通过制表符完成很容易找到。我发现很难记住确切的选项名称是什么,所以这种方法对我有用。

For instance, all I have to remember is that it begins with pd.options

例如,我只需要记住它以 pd.options

pd.options.<TAB>

enter image description here

在此处输入图片说明

Most of the options are available under display

大多数选项都可以在 display

pd.options.display.<TAB>

enter image description here

在此处输入图片说明

From here, I usually output what the current value is like this:

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

pd.options.display.max_rows
60

I then set it to what I want it to be:

然后我将它设置为我想要的:

pd.options.display.max_rows = 100

Also, you should be aware of the context manager for options, which temporarily sets the options inside of a block of code. Pass in the option name as a string followed by the value you want it to be. You may pass in any number of options in the same line:

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

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

You can also reset an option back to its default value like this:

您还可以将选项重置为其默认值,如下所示:

pd.reset_option('display.max_rows')

And reset all of them back:

并将它们全部重置:

pd.reset_option('all')

It is still perfectly good to set options via pd.set_option. I just find using the attributes directly is easier and there is less need for get_optionand set_option.

通过pd.set_option. 我只是发现直接使用属性更容易,并且不需要get_optionset_option

回答by Guilherme Beltramini

It was already pointed in this commentand in this answer, but I'll try to give a more direct answer to the question:

此评论此答案中已经指出了一点,但我将尝试对这个问题提供更直接的答案:

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)

pandas.option_contextis available since pandas 0.13.1 (pandas 0.13.1 release notes). According to this,

pandas.option_context从 pandas 0.13.1 开始可用(pandas 0.13.1 发行说明)。根据

[it] allow[s] you to execute a codeblock with a set of options that revert to prior settings when you exit the with block.

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

回答by Adrien Renaud

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

Does not workin Jupyter!
Instead use:

不工作的Jupyter!
而是使用:

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