是否有一种方法可以在交互或脚本执行模式下扩大输出的显示?

具体来说,我在Pandas DataFrame上使用了describe()函数。当DataFrame是五列(标签)宽时,我得到了我想要的描述性统计数据。然而,如果DataFrame有更多的列,统计数据将被抑制,并返回如下内容:

>> Index: 8 entries, count to max
>> Data columns:
>> x1          8  non-null values
>> x2          8  non-null values
>> x3          8  non-null values
>> x4          8  non-null values
>> x5          8  non-null values
>> x6          8  non-null values
>> x7          8  non-null values

无论有6列还是7列,都给出“8”值。“8”指什么?

我已经尝试过将IDLE窗口拖大,以及增加“配置IDLE”宽度选项,但无济于事。


当前回答

如果你不想打乱你的显示选项,你只是想看到这个特定的列列表,而不是展开你查看的每个数据框架,你可以尝试:

df.columns.values

其他回答

你也可以在循环中尝试:

for col in df.columns: 
    print(col) 

根据v0.18.0的文档,如果你在终端上运行(即,不是IPython notebook, qtconsole或IDLE),让Pandas自动检测你的屏幕宽度并根据它显示的列数进行调整是一个双行程序:

pd.set_option('display.large_repr', 'truncate')
pd.set_option('display.max_columns', 0)

我只用了这三句话:

pd.set_option('display.max_columns', None)
pd.set_option('display.expand_frame_repr', False)
pd.set_option('max_colwidth', -1)

它适用于Anaconda, Python 3.6.5, Pandas 0.23.0和Visual Studio Code 1.26。

似乎前面所有的答案都能解决这个问题。还有一点:你可以使用(auto-complete-able)而不是pd.set_option('option_name'):

pd.options.display.width = None

参见Pandas文档:选项和设置:

选项有一个完整的“虚线风格”,不区分大小写的名称(例如。 display.max_rows)。的属性可以直接获取/设置选项 顶级选项属性: 在[1]中:导入熊猫为pd 在[2]:pd.options.display.max_rows中 [2]: 15 在[3]:pd.options.display中。Max_rows = 999 在[4]:pd.options.display.max_rows中 出[4]:999

[…]

对于max_…参数:

max_rows and max_columns are used in __repr__() methods to decide if to_string() or info() is used to render an object to a string. In case Python/IPython is running in a terminal this can be set to 0 and pandas will correctly auto-detect the width the terminal and swap to a smaller format in case all columns would not fit vertically. The IPython notebook, IPython qtconsole, or IDLE do not run in a terminal and hence it is not possible to do correct auto-detection. ‘None’ value means unlimited. [emphasis not in original]

对于width参数:

以字符为单位的显示宽度。如果Python/IPython在终端中运行,可以将其设置为None, pandas将正确地自动检测宽度。请注意,IPython notebook、IPython qtconsole或IDLE不在终端中运行,因此不可能正确地检测宽度。

这些答案对我都没用。其中一些确实会打印所有列,但看起来会很草率。所有的信息都在那里,但格式不正确。我正在使用Neovim内部的终端,所以我怀疑这是原因。

这个迷你函数确实是我需要的,只是改变df_data在两个地方,它是为你的dataframe名称(col_range被设置为熊猫自然显示,对我来说是5,但它可以更大或更小为你)。

import math
col_range = 5
for _ in range(int(math.ceil(len(df_data.columns)/col_range))):
    idx1 = _*col_range
    idx2 = idx1+col_range
    print(df_data.iloc[:, idx1:idx2].describe())