【问题标题】:Pandas: Setting no. of max rows熊猫:设置编号。最大行数
【发布时间】:2013-05-01 17:28:42
【问题描述】:

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

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

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

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

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

【问题讨论】:

  • 当我在默认(即没有特殊配置文件)笔记本中运行您的代码时,我会得到一个漂亮的打印表格,该表格可滚动显示所有值。仅供参考,我的 pandas.__version__ = 0.9.1(不确定这是否重要)
  • 我指的是普通 shell,不是 ipython
  • 我感觉这可能是 0.11+ 中的一个错误...
  • 嗨,安迪。韦斯已经证实了这一点吗?我在哪里可以提交这个错误?有解决办法吗?
  • 我刚刚提交了 here,我知道 DataFrame repr 的 0.11 版本有一些最后的更改,所以我抄送了错误报告中的那些。会让你知道重新解决方法。

标签: python formatting pandas ipython-notebook


【解决方案1】:

设置display.max_rows:

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

对于旧版本的 pandas (display.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')

【讨论】:

  • +1 为 pd.describe_option('display'),我不知道所有选项
  • Height 现在已被弃用,因此 display.max_rows 选项就足够了。
  • 对于只查看已接受答案的任何人:使用with pd.option_context('display.height', 500, 'display.max_rows', 500): 仅临时设置这些。
  • 不确定这是否是 jupyter 实验室的问题,但这些都不起作用。只有 'display.max_rows' = None 会。
  • @antonavy 我不知道这是否是您的问题,但是当将 display.max_rows 设置为整数时,还需要设置 display.min_rows 以影响截断视图中显示的行数(当max_rows 已超出)。
【解决方案2】:

就像在this answersimilar question 一样,无需破解设置。写起来就简单多了:

print(foo.to_string())

【讨论】:

  • 您不应该将其转换为字符串。这不是安迪要求的。
  • @simtim Andy 询问如何“显示整个数组”。这将做到这一点,并且比接受的答案简单得多
【解决方案3】:

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

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

Release Notes — pandas 0.18.1 documentation

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

【讨论】:

    【解决方案4】:

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

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

    pd.options.<TAB>
    

    display 下提供了大部分选项

    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_optionset_option

    【讨论】:

    • with pd.option_context 是这些答案中最干净的方法;副作用最小。
    • 我想这取决于您在笔记本中执行的操作,但我确实发现上下文管理器非常有用,因此我将它包装在一个函数中并在其中使用了 display(df)
    【解决方案5】:

    已经在this commentthis answer 中指出,但我会尝试更直接地回答这个问题:

    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_context 从 pandas 0.13.1 (pandas 0.13.1 release notes) 开始可用。 根据this

    [它]允许[s]您执行带有一组选项的代码块,当您退出 with 块时,这些选项会恢复为先前的设置。

    【讨论】:

      【解决方案6】:
      pd.set_option('display.max_rows', 500)
      df
      

      在 Jupyter 中不起作用
      而是使用:

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

      【讨论】:

      • github.com/pandas-dev/pandas/issues/… 中解释了为什么会这样。您还必须设置display.min_rows,因为display.max_rows 是阈值,当您显示display.min_rows 角色数量时。我不知道他们是怎么想出这些名字的。
      • 第一行现在似乎可以在没有 head() 技巧的 jupyter notebook 中使用。
      【解决方案7】:

      设置无限行数使用

      即,

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

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

      同样,您可以设置将所有列显示为

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

      现在,如果您使用只有数据框且没有任何头或尾标签的单元格运行 作为

      df
      

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

      【讨论】:

      • 不是max_columns吗?
      • 感谢@igorkf 的更新,我已经做出了更改感谢您阅读我的帖子,学习愉快
      猜你喜欢
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 1970-01-01
      • 2021-12-09
      • 2021-08-17
      • 1970-01-01
      • 2014-06-27
      相关资源
      最近更新 更多