How to stop my pandas data table from being truncated when printed?

16,503

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

Share:
16,503
Brndn
Author by

Brndn

Updated on June 08, 2022

Comments

  • Brndn
    Brndn almost 2 years

    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)
    
  • Brndn
    Brndn almost 6 years
    cheers that worked! I already had the top 2 set to None... adding the width as you suggested rectified the issue. Kind regards!
  • Jeroen
    Jeroen almost 6 years
    Np! Note that this doesn't alter your dataframe, but it alters the way it is displayed.
  • Seth
    Seth about 4 years
    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|displa‌​y.width'). (The argument is a regex - see documentation at pandas.pydata.org/pandas-docs/stable/reference/api/… .)
  • Karl Bartel
    Karl Bartel over 2 years
    There's also a nice context manager for it: pandas.pydata.org/pandas-docs/stable/reference/api/…
  • alper
    alper almost 2 years
    Should I write it on top of the file?
  • Jeroen
    Jeroen almost 2 years
    @alper You should run this before printing a pandas dataframe
  • alper
    alper almost 2 years
    I did but it had no affect on the printed output