在Jupyter Python Notebook中显示所有数据框列

问题:在Jupyter Python Notebook中显示所有数据框列

我想在Jupyter Notebook的数据框中显示所有列。Jupyter显示一些列,并在最后一列中添加点,如下图所示:

如何显示所有列?

I want to show all columns in a dataframe in a Jupyter Notebook. Jupyter shows some of the columns and adds dots to the last columns like in the following picture:

How can I display all columns?


回答 0

尝试如下显示max_columns设置:

import pandas as pd
from IPython.display import display

df = pd.read_csv("some_data.csv")
pd.options.display.max_columns = None
display(df)

要么

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

编辑:熊猫0.11.0向后

不建议使用此功能,但在低于0.11.0的Pandas版本中,该max_columns设置指定如下:

pd.set_printoptions(max_columns=500)

Try the display max_columns setting as follows:

import pandas as pd
from IPython.display import display

df = pd.read_csv("some_data.csv")
pd.options.display.max_columns = None
display(df)

Or

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

Edit: Pandas 0.11.0 backwards

This is deprecated but in versions of Pandas older than 0.11.0 the max_columns setting is specified as follows:

pd.set_printoptions(max_columns=500)

回答 1

我知道这个问题有点老了,但是以下内容在运行pandas 0.22.0和Python 3的Jupyter Notebook中为我工作:

import pandas as pd
pd.set_option('display.max_columns', <number of columns>)

您也可以对行执行相同的操作:

pd.set_option('display.max_rows', <number of rows>)

这样可以节省导入IPython的时间,并且pandas.set_option文档中还有更多选项:https ://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.set_option.html

I know this question is a little old but the following worked for me in a Jupyter Notebook running pandas 0.22.0 and Python 3:

import pandas as pd
pd.set_option('display.max_columns', <number of columns>)

You can do the same for the rows too:

pd.set_option('display.max_rows', <number of rows>)

This saves importing IPython, and there are more options in the pandas.set_option documentation: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.set_option.html


回答 2

适用于大型(但不是太大)DataFrame的Python 3.x

也许是因为我有较旧版本的熊猫,但在Jupyter笔记本上,这对我有用

import pandas as pd
from IPython.core.display import HTML

df=pd.read_pickle('Data1')
display(HTML(df.to_html()))

Python 3.x for large (but not too large) DataFrames

Maybe because I have an older version of pandas but on Jupyter notebook this work for me

import pandas as pd
from IPython.core.display import HTML

df=pd.read_pickle('Data1')
display(HTML(df.to_html()))

回答 3

我建议在上下文管理器中设置显示选项,以使其仅影响单个输出。如果您还想打印“漂亮”的html版本,我将定义一个函数并df使用force_show_all(df)以下命令显示数据框:

from IPython.core.display import display, HTML

def force_show_all(df):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', None):
        display(HTML(df.to_html()))

正如其他人提到的那样,请谨慎地仅在合理大小的数据帧上调用它。

I recommend setting the display options inside a context manager so that it only affects a single output. If you also want to print a “pretty” html-version, I would define a function and display the dataframe df using force_show_all(df):

from IPython.core.display import display, HTML

def force_show_all(df):
    with pd.option_context('display.max_rows', None, 'display.max_columns', None, 'display.width', None):
        display(HTML(df.to_html()))

As others have mentioned, be cautious to only call this on a reasonably-sized dataframe.


回答 4

您可以使用pandas.set_option()作为列,您可以指定以下任何选项

pd.set_option("display.max_rows", 200)
pd.set_option("display.max_columns", 100)
pd.set_option("display.max_colwidth", 200)

对于完整的打印列,您可以这样使用

import pandas as pd
pd.set_option('display.max_colwidth', -1)
print(words.head())

you can use pandas.set_option(), for column, you can specify any of these options

pd.set_option("display.max_rows", 200)
pd.set_option("display.max_columns", 100)
pd.set_option("display.max_colwidth", 200)

For full print column, you can use like this

import pandas as pd
pd.set_option('display.max_colwidth', -1)
print(words.head())


回答 5

如果要显示所有行设置如下

pd.options.display.max_rows = None

如果要显示所有列,例如波纹管

pd.options.display.max_columns = None

If you want to show all the rows set like bellow

pd.options.display.max_rows = None

If you want to show all columns set like bellow

pd.options.display.max_columns = None