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

具体来说,我在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

其他回答

您可以使用print df.describe().to_string()强制它显示整个表。你可以像这样对任何数据帧使用to_string()。description的结果只是一个DataFrame本身。)

8是DataFrame中包含“description”的行数(因为describe计算8个统计值,最小值,最大值,平均值等)。

使用以下方法设置列的最大宽度:

pd.set_option('max_colwidth', 800)

这个特定的语句将每列的最大宽度设置为800像素。

这不是严格意义上的答案,但是让我们记住我们可以df.describe().transpose()或者df.head(n).transpose(),或者df.tail(n).transpose()。

我还发现,当标题是结构化的时,将它们作为列来阅读更容易:

header1_xxx,

header2_xxx,

header3_xxx,

我认为终端和应用程序处理垂直滚动更自然,如果这是必要的转置后。

标头通常比它们的值大,将它们全部放在一列(索引)中可以最大限度地减少它们对总表宽度的影响。

最后,其他的df描述也可以合并,这里有一个可能的想法:

def df_overview(df: pd.DataFrame, max_colwidth=25, head=3, tail=3):
    return(
        df.describe([0.5]).transpose()
        .merge(df.dtypes.rename('dtypes'), left_index=True, right_index=True)
        .merge(df.head(head).transpose(), left_index=True, right_index=True)
        .merge(df.tail(tail).transpose(), left_index=True, right_index=True)
        .to_string(max_colwidth=max_colwidth, float_format=lambda x: "{:.4G}".format(x))
    )

下面的行足以显示一个数据框架中的所有列。

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

似乎前面所有的答案都能解决这个问题。还有一点:你可以使用(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不在终端中运行,因此不可能正确地检测宽度。