熊猫:设置编号。最大行数

新手上路,请多包涵

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

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

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

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

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

原文由 Andy 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 738
2 个回答

设置 display.max_rows

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

对于旧版本的 pandas (<=0.11.0),您需要同时更改 display.heightdisplay.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')

原文由 Wouter Overmeire 发布,翻译遵循 CC BY-SA 4.0 许可协议

就个人而言,我喜欢直接使用赋值语句设置选项,因为借助 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 的需求也更少。

原文由 Ted Petrou 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
logo
Stack Overflow 翻译
子站问答
访问
宣传栏