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

具体来说,我在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”宽度选项,但无济于事。


当前回答

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

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

其他回答

你可以简单地执行以下步骤,

您可以更改Pandas max_columns特性的选项,如下所示: 进口熊猫作为pd pd.options.display。Max_columns = 10 (这允许显示10个列,您可以根据需要更改。) 像这样,你可以改变行数,你需要显示如下(如果你需要改变最大行数): pd.options.display。Max_rows = 999 (这允许一次打印999行。)

如需更改Pandas的不同选项/设置,请参考文档。

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

import pandas as pd
pd.set_option('display.max_columns', 100)
pd.set_option('display.width', 1000)

SentenceA = "William likes Piano and Piano likes William"
SentenceB = "Sara likes Guitar"
SentenceC = "Mamoosh likes Piano"
SentenceD = "William is a CS Student"
SentenceE = "Sara is kind"
SentenceF = "Mamoosh is kind"


bowA = SentenceA.split(" ")
bowB = SentenceB.split(" ")
bowC = SentenceC.split(" ")
bowD = SentenceD.split(" ")
bowE = SentenceE.split(" ")
bowF = SentenceF.split(" ")

# Creating a set consisting of all words

wordSet = set(bowA).union(set(bowB)).union(set(bowC)).union(set(bowD)).union(set(bowE)).union(set(bowF))
print("Set of all words is: ", wordSet)

# Initiating dictionary with 0 value for all BOWs

wordDictA = dict.fromkeys(wordSet, 0)
wordDictB = dict.fromkeys(wordSet, 0)
wordDictC = dict.fromkeys(wordSet, 0)
wordDictD = dict.fromkeys(wordSet, 0)
wordDictE = dict.fromkeys(wordSet, 0)
wordDictF = dict.fromkeys(wordSet, 0)

for word in bowA:
    wordDictA[word] += 1
for word in bowB:
    wordDictB[word] += 1
for word in bowC:
    wordDictC[word] += 1
for word in bowD:
    wordDictD[word] += 1
for word in bowE:
    wordDictE[word] += 1
for word in bowF:
    wordDictF[word] += 1

# Printing term frequency

print("SentenceA TF: ", wordDictA)
print("SentenceB TF: ", wordDictB)
print("SentenceC TF: ", wordDictC)
print("SentenceD TF: ", wordDictD)
print("SentenceE TF: ", wordDictE)
print("SentenceF TF: ", wordDictF)

print(pd.DataFrame([wordDictA, wordDictB, wordDictB, wordDictC, wordDictD, wordDictE, wordDictF]))

输出:

   CS  Guitar  Mamoosh  Piano  Sara  Student  William  a  and  is  kind  likes
0   0       0        0      2     0        0        2  0    1   0     0      2
1   0       1        0      0     1        0        0  0    0   0     0      1
2   0       1        0      0     1        0        0  0    0   0     0      1
3   0       0        1      1     0        0        0  0    0   0     0      1
4   1       0        0      0     0        1        1  1    0   1     0      0
5   0       0        0      0     1        0        0  0    0   1     1      0
6   0       0        1      0     0        0        0  0    0   1     1      0

这些答案对我都没用。其中一些确实会打印所有列,但看起来会很草率。所有的信息都在那里,但格式不正确。我正在使用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())

我只用了这三句话:

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。