Python 是否有一种简洁的方法可以仅显示当前命令的 Pandas 中的所有行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30876193/
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
Is there a concise way to show all rows in pandas for just the current command?
提问by MMelnicki
Sometimes I want to show all of the rows in a pandas DataFrame, but only for a single command or code-block.
有时我想显示 Pandas DataFrame 中的所有行,但只显示单个命令或代码块。
Of course I can set the "max_rows" display option to a large number, but then I have to repeat the command afterwards in order to revert to my preferred setting. (I like 12 rows max, personally).
当然,我可以将“max_rows”显示选项设置为较大的数字,但之后我必须重复该命令才能恢复到我的首选设置。(我个人最喜欢 12 行)。
pd.options.display.max_rows=1000
myDF
pd.options.display.max_rows=12
That's annoying.
这很烦人。
I read in the documentation that I can use the pd.option_context() function to accomplish this if I combine my command with a "with" statement:
我在文档中读到,如果我将命令与“with”语句结合使用,我可以使用 pd.option_context() 函数来完成此操作:
with pd.option_context("display.max_rows", 1000): myDF
I couldn't get that to work, (no output is returned). But I think such a solution would stillbe too much typing for routine incidental usage!
我无法让它工作,(没有输出返回)。但我认为这样的解决方案对于日常偶然使用来说仍然会打太多字!
I wish there was some quick pythonic way to override the display options!
Does one exist? Have I overlooked something?
我希望有一些快速的 Pythonic 方法来覆盖显示选项!
一个存在吗?我是否忽略了什么?
I like how one can alter the # of rows that the .head() function outputs by passing it an argument for the # of rows, but it still must be lower than the "display.max_rows" setting...
我喜欢如何改变 .head() 函数输出的行数,方法是向它传递一个行数的参数,但它仍然必须低于“display.max_rows”设置......
I know I couldkeep the "display.max_rows" setting really high all the time, and then tack a .head(12) function on most of the time, but I think most people would agree on how annoying that would be.
我知道我可以一直保持“display.max_rows”设置非常高,然后在大部分时间添加一个 .head(12) 函数,但我想大多数人都会同意这会有多烦人。
I am indeed aware that one can view all (or most of?) the values in a pandas Series by passing it to a core function such as list(). But that is tricky to do with a DF. Furthermore, it's hard to read when it's not in a tabular format.
我确实知道可以通过将 pandas 系列中的值传递给诸如 list() 之类的核心函数来查看所有(或大部分?)值。但这对 DF 来说很棘手。此外,当它不是表格格式时很难阅读。
Similar to the solution for my first question, I imagine there's probably a way to write my own function (to be placed in a startup script), but I'm not sure the best way to write it.
类似于我的第一个问题的解决方案,我想可能有一种方法可以编写我自己的函数(放置在启动脚本中),但我不确定编写它的最佳方法。
采纳答案by Ami Tavory
You could write a function that explicitly calls display
您可以编写一个显式调用的函数 display
E.g., consider this function:
例如,考虑这个函数:
from IPython.display import display
def show_more(df, lines):
foo = 1
display(df)
foo = 2
When I call the function (just tried it):
当我调用该函数时(刚刚尝试过):
>> show_more(df, 1000)
... # <- Shows here the DF
then it displays the dataframe, even though the line foo = 2
is executed after. It follows that you can set the options instead of the line foo = 1
and unset it in the line foo = 2
. In fact, you can just use the context manager from your question, probably.
然后显示数据帧,即使该线foo = 2
被执行之后。因此,您可以设置选项而不是行foo = 1
并在行中取消设置foo = 2
。事实上,您可能可以从您的问题中使用上下文管理器。
回答by Stop harming Monica
This won't display anything because it does not return anything:
这不会显示任何内容,因为它不返回任何内容:
with pd.option_context("display.max_rows", 1000): myDF
Calling display
inside the with
block should work:
display
在with
块内调用应该可以工作:
with pd.option_context("display.max_rows", 1000):
display(myDF)
回答by tgolubch
This seems to work as expected in pandas 0.22.0 (importing only pandas, without IPython):
这似乎在 Pandas 0.22.0 中按预期工作(仅导入 Pandas,不导入 IPython):
import pandas as pd
with pd.option_context("display.max_rows", 1000): myDF
Presumably that's because the default behaviour is to return the repr of myDF. IDEs may well override this.
大概是因为默认行为是返回 myDF 的 repr。IDE 很可能会覆盖这一点。
If that's too much typing, then a straightforward print to the terminal also works when wrapped in a function:
如果输入太多,那么在包装在函数中时,直接打印到终端也有效:
from __future__ import print_statement # for python2
def show_rows(df, nrows=1000):
with pd.option_context("display.max_rows", nrows): print(df)
Edit: calling show_rows(df)will by default print the first 1000 rows of your dataframe dfto standard output.
编辑:调用show_rows(df)将默认将数据帧df的前 1000 行打印到标准输出。
回答by Hardik Sondagar
You can use following helper function to print full data frame and set max_rows
to normal after printing.
您可以使用以下辅助功能打印完整的数据框并max_rows
在打印后设置为正常。
def print_full(df):
import pandas as pd
pd.set_option('display.max_rows', len(df))
print(df)
pd.reset_option('display.max_rows')
回答by Yuri Feldman
One-liner to force display all rows (in jupyter):
单行强制显示所有行(在 jupyter 中):
import IPython.display
IPython.display.HTML(df.to_html())