pandas IPython - 有打印默认打印头和尾长变量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22868254/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 21:53:41  来源:igfitidea点击:

IPython - have print default to print head & tail on long variables

pythonnumpypandasipython

提问by schodge

(For working in IPython only, either terminal or QTConsole, not Notebook) Is there a way to make a regular print statement (yes, 2.7) automatically print the head and tail (or even just the head) of a variable if it is over some arbitrary size?

(仅适用于 IPython,终端或 QTConsole,而不是 Notebook)有没有办法使常规打印语句(是的,2.7)自动打印变量的头部和尾部(甚至只是头部),如果它结束了一些任意大小?

If I print dataframeon a pandas dataframe that is too big, pandas automatically just prints the head. I'd like it work that way on lists and numpy arrays too, so that the next time I accidentally print a giant array out by accident I don't wind up with 100 pages of numbers. (I'm sure I could write a function to do this, but I reflexively use print, so I'm wondering if there's a way to control how much IPython will display - really, I'd rather change what the console displays instead of what the program is doing.)

如果我print dataframe在太大的Pandas数据帧上,Pandas会自动打印头部。我也希望它在列表和 numpy 数组上也能这样工作,这样下次我不小心打印出一个巨大的数组时,我就不会得到 100 页的数字。(我确定我可以编写一个函数来执行此操作,但我会条件反射地使用打印,所以我想知道是否有办法控制 IPython 将显示多少 - 实际上,我宁愿更改控制台显示的内容而不是程序在做什么。)

采纳答案by Dima Tisnek

You can't really do much with printstatement, but you can do something about ipython's output, consider these to commands:

你真的不能用printstatement做太多事情,但是你可以对ipython的输出做一些事情,考虑这些命令:

In [13]: x = range(3)

In [14]: x
Out[14]: [0, 1, 2]

In [15]: print x
[0, 1, 2]

First, xassigns Out[14]to value of the expression and that Out[14]is then displayed.

首先,x分配Out[14]给表达式的值,Out[14]然后显示。

You can hack ipythonto display it differently. From what I can tell, ipythonuses pprintinternally or a copy thereof. Simply monkey-patching pprint.pprint/pprint.pformat/pprint.PrettyPrinterdoesn't work though. You'd need to hack ipython proper.

你可以破解ipython以不同的方式显示它。据我所知,内部ipython使用pprint或其副本。不过,简单的猴子修补pprint.pprint/pprint.pformat/pprint.PrettyPrinter是行不通的。你需要正确地破解 ipython。

Second, print xgoes straight to the heart of Python. In python 2.x you cannot change what printdoes. In python 3.x print()is just a function and you can monkey-patch it.

其次,print x直击 Python 的核心。在 python 2.x 中,你不能改变什么print。在 python 3.xprint()中只是一个函数,你可以给它打补丁。

If what you print is a object of your own class, feel free to redefine __str__and __repr__like numpy does:

如果您打印的是您自己的类的对象,请随意重新定义__str____repr__像 numpy 一样:

numpy.set_printoptions(threshold=2)
numpy.array(range(10)).__str__()
'[0 1 2 ..., 7 8 9]'
print numpy.array(range(10))
[0 1 2 ..., 7 8 9]

Finally, ipythoncomes with %pagemagic command, you can issue %page xand if representation of xis large enough, you'll be dropped to lessor similar pager where you can view this text representation one page at a time.

最后,ipython附带%page魔术命令,您可以发出%page x,如果表示x足够大,您将被放置到less或类似的寻呼机,您可以一次一页地查看此文本表示。

P.S. ipythonis extensible via "traits," it's still back magic for me, but perhaps you can solve first case using traits.

PSipython可以通过“特征”进行扩展,对我来说它仍然是魔法,但也许您可以使用特征来解决第一种情况。