如何防止我的pandas数据表在打印时被截断?

22

我编写了一段代码,它读取两个字符串,然后比较它们是否含有相似的单词。接着会生成一个包含数据的表格。

我的问题是它总是被分成两部分。我需要纠正这个问题,以便将其合并到HTML中。感谢您提前的任何帮助!:)

我还尝试了仅打印顶部行。

top row

完整代码:

import string
from os import path

import pandas as pd
pd.set_option('display.max_columns', None) #prevents trailing elipses
pd.set_option('display.max_rows', None)
import os.path

BASE = os.path.dirname(os.path.abspath(__file__))

file1 = open(os.path.join(BASE, "samp.txt"))
sampInput=file1.read().replace('\n', '')
file2 = open(os.path.join(BASE, "ref.txt"))
refInput=file2.read().replace('\n', '')

sampArray = [word.strip(string.punctuation) for word in sampInput.split()]
refArray = [word.strip(string.punctuation) for word in refInput.split()]

out=pd.DataFrame(index=sampArray,columns=refArray)

for i in range(0, out.shape[0]): #from 0 to total number of rows
        for word in refArray: #for each word in the samplearray

                df1 = out.iloc[0, 0:16].copy()
                top = out.ix[:1, :17]

                out.ix[i,str(word)] = out.index[i].count(str(word))
#print(out)
print(top)
#print(df1)

它并没有分成两个数据框;当数据框有太多列时,它会自动打印到另一行(请参见反斜杠)。您可以尝试使用“from IPython.display import display”然后使用“display(top)”而不是print。 - xyzjayne
我明白你的意思。谢谢!我尝试了你建议的方法,但不幸的是它仍然输出相同的结果。主要问题是这会影响我将数据呈现到网页上的表格中的能力吗? - Brndn
如果您想要导出一个图表,这个链接可能会有所帮助:https://dev59.com/xFsV5IYBdhLWcg3w6yYh - xyzjayne
谢谢,我会开始处理的 :) - Brndn
它起作用了,非常感谢xyzjayne xD。 - Brndn
1个回答

42

您可以设置如何显示数据框的选项:

pd.set_option('display.max_rows', 500)
pd.set_option('display.max_columns', 500)
pd.set_option('display.width', 150)

如果您在打印前添加此内容,您的数据框将以您期望的格式打印


1
太好了,它起作用了!我已经将前两个设置为“None”了...您建议添加宽度后问题得到解决。顺祝商祺! - Brndn
2
没问题!请注意,这不会改变您的数据框,但它会改变其显示方式。 - Jeroen
3
而且,如果您只想暂时设置选项,您可以使用 pd.reset_option('display.max_rows|display.max_columns|display.width') 将其改回默认值。 (参数是一个正则表达式 - 请参阅 https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.reset_option.html 上的文档。) - Seth
还有一个很好的上下文管理器:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.option_context.html - Karl Bartel
1
我尝试了,但对打印输出没有影响。 - alper
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接