是否有一种简洁的方法来仅显示当前命令来显示 pandas 中的所有行? [英] Is there a concise way to show all rows in pandas for just the current command?

查看:72
本文介绍了是否有一种简洁的方法来仅显示当前命令来显示 pandas 中的所有行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候我想显示pandas DataFrame中的所有行,但仅显示单个命令或代码块.

Sometimes I want to show all of the rows in a pandas DataFrame, but only for a single command or code-block.

我当然可以将"max_rows"显示选项设置为一个较大的数字,但是之后我必须重复此命令才能恢复到我的首选设置. (我个人最喜欢最多12行).

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).

pd.options.display.max_rows=1000
myDF
pd.options.display.max_rows=12

这很烦人.

我在文档中读到,如果我将命令与"with"语句结合使用,则可以使用pd.option_context()函数来完成此操作:

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("display.max_rows", 1000): myDF

我无法使它正常工作((没有输出返回). 但是我认为这样的解决方案仍然对于常规的偶然用法来说还是太过打字了!

I couldn't get that to work, (no output is returned). But I think such a solution would still be too much typing for routine incidental usage!

我希望有一些快速的Python方法来覆盖显示选项!
是否存在?我忽略了什么吗?

I wish there was some quick pythonic way to override the display options!
Does one exist? Have I overlooked something?

我喜欢如何通过向.head()函数传递#行的参数来更改.head()函数输出的行数,但是它仍然必须低于"display.max_rows"设置...

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...

我知道我可以一直保持很高的"display.max_rows"设置,然后在大多数情况下使用.head(12)函数,但是我认为大多数人会同意这会带来多大的烦恼.

I know I could keep 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.

我确实知道,可以将熊猫系列的值传递给诸如list()之类的核心函数,从而查看所有(或大多数?)值.但这对于DF很棘手.此外,当它不是表格格式时,很难阅读.

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.

类似于第一个问题的解决方案,我想可能有一种方法可以编写我自己的函数(放在启动脚本中),但我不确定编写该脚本的最佳方法.

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.

推荐答案

您可以编写一个显式调用

You could write a function that explicitly calls display

例如,考虑以下功能:

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

然后,即使行foo = 2之后执行,它也会显示数据帧.因此,您可以设置选项而不是行foo = 1并在行foo = 2中取消设置.实际上,也许您可​​以只使用问题中的上下文管理器.

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.

这篇关于是否有一种简洁的方法来仅显示当前命令来显示 pandas 中的所有行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆