20

I've written code that reads in two strings then compares them for similar words. A table is then produced with the data.

My problem is that it keeps splitting into two. I need to rectify this to be able to incorporate this into HTML. I'd appreciate any help, and thanks in advance! :)

I tried printing just the top row also.

top row

Full code:

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)
xyzjayne
  • 1,331
  • 9
  • 25
Brndn
  • 676
  • 1
  • 7
  • 21
  • It's not split into two dataframes; when the dataframe has too many columns it just automatically prints on a different line (see the backslash). You can try ````from IPython.display import display```` and then ````display(top)```` instead of print. – xyzjayne Jul 13 '18 at 13:31
  • I get you. Thank you! I tried what you recommended but it gives the same output unfortunately. The main thing is will this impact my ability to render the data onto a webpage into a table? – Brndn Jul 13 '18 at 13:39
  • If you are trying to export a graph, this might help: https://stackoverflow.com/questions/35634238/how-to-save-a-pandas-dataframe-table-as-a-png – xyzjayne Jul 13 '18 at 13:41
  • thanks a lot I'll get to it :) – Brndn Jul 13 '18 at 13:44
  • it worked thanks a lot xyzjayne xD – Brndn Jul 13 '18 at 14:27

1 Answers1

39

You can set options on how to display your dataframes:

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

If you add this before you print anything, your dataframe will be printed in the format you'd expect

Jeroen
  • 857
  • 10
  • 18
  • 1
    cheers that worked! I already had the top 2 set to None... adding the width as you suggested rectified the issue. Kind regards! – Brndn Jul 13 '18 at 13:41
  • 2
    Np! Note that this doesn't alter your dataframe, but it alters the way it is displayed. – Jeroen Jul 13 '18 at 13:43
  • 3
    And, if you want to set the options onlytemporarily, you can change it back to the default afterwards with `pd.reset_option('display.max_rows|display.max_columns|display.width')`. (The argument is a regex - see documentation at https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.reset_option.html .) – Seth Feb 18 '20 at 20:37
  • There's also a nice context manager for it: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.option_context.html – Karl Bartel Oct 20 '21 at 13:54
  • Should I write it on top of the file? – alper May 15 '22 at 13:38
  • @alper You should run this before printing a pandas dataframe – Jeroen May 16 '22 at 14:04
  • 1
    I did but it had no affect on the printed output – alper May 16 '22 at 20:36