【问题标题】:How to stop my pandas data table from being truncated when printed?如何阻止我的熊猫数据表在打印时被截断?
【发布时间】:2018-07-13 12:54:03
【问题描述】:

我编写了读取两个字符串的代码,然后将它们进行比较以获得相似的单词。然后用数据生成一个表。

我的问题是它一直分成两部分。我需要纠正这一点才能将其合并到 HTML 中。我将不胜感激任何帮助,并在此先感谢! :)

我也尝试只打印第一行。

完整代码:

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) 而不是打印。
  • 我明白了。谢谢!我尝试了您推荐的方法,但不幸的是它给出了相同的输出。主要是这会影响我将数据呈现到网页上的能力吗?
  • 如果您尝试导出图表,这可能会有所帮助:stackoverflow.com/questions/35634238/…
  • 非常感谢,我会去的 :)
  • 非常感谢 xyzjayne xD

标签: python django pandas dataframe python-3.7


【解决方案1】:

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

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

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

【讨论】:

  • 干杯!我已经将前 2 个设置为无...按照您的建议添加宽度可以解决问题。亲切的问候!
  • 不!请注意,这不会改变您的数据框,但会改变它的显示方式。
  • 而且,如果您只想暂时设置选项,您可以在之后使用pd.reset_option('display.max_rows|display.max_columns|display.width') 将其更改回默认值。 (参数是一个正则表达式 - 请参阅pandas.pydata.org/pandas-docs/stable/reference/api/… 的文档。)
  • 还有一个不错的上下文管理器:pandas.pydata.org/pandas-docs/stable/reference/api/…
猜你喜欢
  • 1970-01-01
  • 2018-02-16
  • 2021-03-12
  • 1970-01-01
  • 2016-11-25
  • 2017-01-01
  • 2014-11-30
  • 1970-01-01
  • 2014-05-22
相关资源
最近更新 更多