pandas ipython笔记本熊猫最大允许列

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

ipython notebook pandas max allowable columns

pythonpandasipythonipython-notebook

提问by yoshiserry

I have a simple csv file with ten columns!

我有一个包含十列的简单 csv 文件!

When I set the following option in the notebook and print my csv file (which is in a pandas dataframe) it doesn't print all the columns from left to right, it prints the first two, the next two underneath and so on.

当我在笔记本中设置以下选项并打印我的 csv 文件(位于 Pandas 数据框中)时,它不会从左到右打印所有列,而是打印前两列,下两列,依此类推。

I used this option, why isn't it working?

我使用了这个选项,为什么它不起作用?

pd.option_context("display.max_rows",1,"display.max_columns",100)

Even this doesn't seem to work:

即使这似乎不起作用:

pandas.set_option('display.max_columns', None)

回答by Jakob

I assume you want to display your data in the notebook than the following options work fine for me (IPython 2.3):

我假设你想在笔记本中显示你的数据,而不是以下选项对我来说很好(IPython 2.3):

import pandas as pd
from IPython.display import display
data = pd.read_csv('yourdata.txt')

Either directly set the option

要么直接设置选项

pd.options.display.max_columns = None
display(data)

Or, use the set_option method you showed actually works fine as well

或者,使用您展示的 set_option 方法也可以正常工作

pd.set_option('display.max_columns', None)
display(data)

If you don't want to set this options for the whole script use the context manager

如果您不想为整个脚本设置此选项,请使用上下文管理器

with pd.option_context('display.max_columns', None):
    display(data)

If this doesn't help, you might give a minimal example to reproduce your issue.

如果这没有帮助,您可能会给出一个最小的示例来重现您的问题。

回答by scottclowe

You can also display all the data by asking pandas to return HTML markup, and then having IPython render the HTML table.

您还可以通过让 pandas 返回 HTML 标记,然后让 IPython 呈现 HTML 表来显示所有数据。

import pandas as pd
from IPython.display import HTML
data = pd.read_csv('yourdata.csv')
HTML(data.to_html())

Using IPython 3.0.0 and Python 3.4, I found that display(data)as described by @Jakob will render as a table with up/down and left/right scroll bars, but the table is still wider than the cell and some columns are off-screen to the right. To see all the data, one must collapse the cell - which adds scroll bars. Consequently you have a scrolling box in a scrolling box, which is not ideal as you have to shift focus between the doubled-up scroll bars to navigate all the way through the data.

使用 IPython 3.0.0 和 Python 3.4,我发现display(data)如@Jakob 所述,将呈现为带有向上/向下和向左/向右滚动条的表格,但表格仍然比单元格宽,并且某些列在屏幕外正确的。要查看所有数据,必须折叠单元格 - 这会添加滚动条。因此,您在滚动框中有一个滚动框,这并不理想,因为您必须在双倍滚动条之间移动焦点才能一直浏览数据。

Using the HTML method, you render the enormous table as-is without any scroll bars. This cell can then be collapsed down to show only a single vertical and horizontal bar, which is more user-friendly.

使用 HTML 方法,您可以在没有任何滚动条的情况下按原样呈现巨大的表格。然后可以折叠此单元格以仅显示单个垂直和水平条,这对用户更加友好。

The caveat to using HTML is the table takes longer to render. I was only using a ~150x50 matrix and the speed difference was noticeable, but not inconvenient. If you have an enormous table, don't use this method to display the entire thing at once. That said, if you do have an enormous table, rendering the whole thing at once is obviously going to be a bad idea however you try to do it.

使用 HTML 的警告是表格需要更长的时间来呈现。我只使用了一个 ~150x50 的矩阵,速度差异很明显,但并不方便。如果您有一张巨大的桌子,请不要使用此方法一次显示整个内容。也就是说,如果你确实有一张巨大的桌子,那么一次渲染整个东西显然是一个坏主意,不管你怎么做。

回答by MasterAir

I found this question as one of the first hits on Google. In jupyter lab,

我发现这个问题是 Google 上的第一个热门问题。在 jupyter 实验室中,

pandas.set_option("display.max_columns", None)

Now seems to work fine - my example was 32 columns, it used to be truncated and is not any more.

现在似乎工作正常 - 我的示例是 32 列,它曾经被截断,现在不再。