Pandas:设置最大行数
- 2025-02-20 09:23:00
- admin 原创
- 24
问题描述:
我在查看以下内容时遇到问题DataFrame
:
n = 100
foo = DataFrame(index=range(n))
foo['floats'] = np.random.randn(n)
foo
问题是它不会在 ipython 笔记本中默认打印所有行,但我必须切片才能查看结果行。即使以下选项也不会改变输出:
pd.set_option('display.max_rows', 500)
有人知道如何显示整个数组吗?
解决方案 1:
放display.max_rows
:
pd.set_option('display.max_rows', 500)
对于旧版本的 pandas (<=0.11.0),您需要同时更改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')
解决方案 2:
pd.set_option('display.max_rows', 500)
df
在 Jupyter 中不起作用
!
请改用:
pd.set_option('display.max_rows', 500)
df.head(500)
解决方案 3:
就我个人而言,我喜欢直接使用赋值语句设置选项,因为借助 iPython,通过 Tab 补全功能可以轻松找到选项。我发现很难记住确切的选项名称,所以这种方法对我来说很有效。
例如,我只需记住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_option
和set_option
。
解决方案 4:
我不知道为什么没人提到这件事。
您还应该设置'display.min_rows'
。
pd.set_option('display.min_rows', 500) # <-add this!
pd.set_option('display.max_rows', 500)
如果total number of rows
> display.max_rows
,
那么仅设置display.max_rows
是行不通的。
(是的,这很令人困惑。应该修改。)
解决方案 5:
此评论和此答案中已经指出了这一点,但我会尝试对这个问题给出更直接的回答:
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 发行说明)。据此,
[它]允许您执行带有一组选项的代码块,当您退出 with 块时,这些选项将恢复到以前的设置。
解决方案 6:
设置无限行数使用
没有任何
IE,
pd.set_option('display.max_columns', None)
现在笔记本将显示笔记本内所有数据集的所有行;)
类似地,您可以设置为显示所有列
pd.set_option('display.max_rows', None)
现在如果你使用只包含数据框而没有任何头或尾标签的单元格来运行
df
然后它将显示数据框中的所有行和列df
解决方案 7:
我将使用上下文管理器来设置这些选项,以便我可以控制哪个 df 应该受到影响。
with pd.option_context('display.min_rows', 50, 'display.max_columns', None):
display(df)
另外,不要display.max_rows
使用display.min_rows
。这应该可以在不设置 的情况下工作display.max_rows
。
解决方案 8:
正如 @hanleyhansen 在评论中指出的那样,从 0.18.1 版本开始,该display.height
选项已被弃用,并表示“改用display.max_rows
”。因此,您只需像这样配置它:
pd.set_option('display.max_rows', 500)
请参阅发行说明 — pandas 0.18.1 文档:
已弃用的 display.height,display.width 现在只是一个格式化选项,不控制摘要的触发,类似于 <0.11.0。
解决方案 9:
正如对类似问题的回答一样,无需破解设置。这样写要简单得多:
print(foo.to_string())
解决方案 10:
pd.options.display.max_rows = 无
这将显示所有行
- 2025年20款好用的项目管理软件推荐,项目管理提效的20个工具和技巧
- 2024年开源项目管理软件有哪些?推荐5款好用的项目管理工具
- 2024年常用的项目管理软件有哪些?推荐这10款国内外好用的项目管理工具
- 项目管理软件有哪些?推荐7款超好用的项目管理工具
- 项目管理软件有哪些最好用?推荐6款好用的项目管理工具
- 项目管理软件哪个最好用?盘点推荐5款好用的项目管理工具
- 项目管理软件排行榜:2024年项目经理必备5款开源项目管理软件汇总
- 项目管理必备:盘点2024年13款好用的项目管理软件
- 项目管理软件有哪些,盘点推荐国内外超好用的7款项目管理工具
- 2024项目管理软件排行榜(10类常用的项目管理工具全推荐)