204

I am struggling with the seemingly very simple thing. I have a pandas data frame containing very long string.

df = pd.DataFrame({'one' : ['one', 'two', 
      'This is very long string very long string very long string veryvery long string']})

Now when I try to print the same, I do not see the full string I rather see only part of the string.

I tried following options

  • using print(df.iloc[2])
  • using to_html
  • using to_string
  • One of the Stack Overflow answers suggested to increase column width by using pandas display option, that did not work either.
  • I also did not get how set_printoptions would solve this.
bad_coder
  • 11,289
  • 20
  • 44
  • 72
Yantraguru
  • 3,604
  • 3
  • 18
  • 21

9 Answers9

268

You can use options.display.max_colwidth to specify you want to see more in the default representation:

In [2]: df
Out[2]:
                                                 one
0                                                one
1                                                two
2  This is very long string very long string very...

In [3]: pd.options.display.max_colwidth
Out[3]: 50

In [4]: pd.options.display.max_colwidth = 100

In [5]: df
Out[5]:
                                                                               one
0                                                                              one
1                                                                              two
2  This is very long string very long string very long string veryvery long string

And indeed, if you just want to inspect the one value, by accessing it (as a scalar, not as a row as df.iloc[2] does) you also see the full string:

In [7]: df.iloc[2,0]    # or df.loc[2,'one']
Out[7]: 'This is very long string very long string very long string veryvery long string'
joris
  • 133,120
  • 36
  • 247
  • 202
  • 2
    df.iloc[2,0] -- doesnt work - I will print first and third row and cutted( – Rocketq Feb 04 '18 at 13:44
  • It does work, if you want element of third row and first column. If you want something else, please open a new question. – joris Feb 04 '18 at 14:38
  • It works, thx! I have a list of zip codes in 1 column and use apply to find the min and max column wise using a function: df['zipcodeMinMax'] = df.loc[:,['zipcodeList']].apply(createMinMaxZipcode, axis=1). So within the function I do to_string on the series (stringZipcodes = zipcodeList.to_string(header=False, index=False)). The longer lists of zipcodes resulted in 3 dots at the end e.g. "1111...". Indeed this does not happen if you select the values based on index and column (as a scalar). My question: why this behaviour? Setting display options to influence apply seems strange to me? Thx! – Wouter Feb 15 '18 at 15:11
  • FYI I added this to make sure the colwidth is sufficient for the length of the strings in the series: colwidth = 500 pd.set_option('max_colwidth',colwidth) if df['zipcodeList'].str.len().max() > colwidth: raise ValueError ('The max width is less than the max length of the string of zipcodes') – Wouter Feb 15 '18 at 15:26
  • 2
    @Wouter if you have a different question, better to ask a new one instead of commenting here – joris Feb 15 '18 at 17:51
  • 2
    This does not work for a bigger string like a paragraph with multiple lines. – devssh Jun 11 '18 at 06:43
  • If you want a more useable format that breaks long strings (or str representations of lists, etc) into multiple lines [@omnesia's answer](https://stackoverflow.com/a/52691689/623735) is your ticket to success. – hobs Sep 22 '20 at 20:42
105

Use pd.set_option('display.max_colwidth', None) for automatic linebreaks and multi-line cells.

This is a great resource on how to use jupyters display with pandas to the fullest.


Edited: Used to be pd.set_option('display.max_colwidth', -1).

omnesia
  • 1,992
  • 1
  • 15
  • 14
28

Another, pretty simple approach is to call list function:

list(df['one'][2])
# output:
['This is very long string very long string very long string veryvery long string']

No worth to mention, that is not good to convent to list the whole columns, but for a simple line - why not

Rocketq
  • 5,423
  • 23
  • 75
  • 126
  • 1
    If you have been whittling down your df based on some search criteria, and it is down to a single line, this doesn't work. It's the simplest way, for debugging needs anyway, and I wish it would work, but I don't know why it doesn't. You get a "*** KeyError: 0". I'm guessing it has to do with being like a "scalar" when there's only one value. – Starman Nov 16 '18 at 21:14
  • As of today, this returns all the characters with a query into the df that returns two cells with 127 characters, that I've been frustrated in trying to get at. If that helps anyone – avirr Nov 01 '19 at 23:14
  • 1
    It can be like this too: `list(df['one'])[2]` - only get the string, not as a list! – user3503711 Nov 30 '22 at 15:09
19

Another easier way to print the whole string is to call values on the dataframe.

df = pd.DataFrame({'one' : ['one', 'two', 
      'This is very long string very long string very long string veryvery long string']})

print(df.values)

The Output will be

[['one']
 ['two']
 ['This is very long string very long string very long string veryvery long string']]
bigbounty
  • 16,526
  • 5
  • 37
  • 65
8

Just add the following line to your code before print.

 pd.options.display.max_colwidth = 90  # set a value as your need

You can simply do the following steps for setting other additional options,

  • You can change the options for pandas max_columns feature as follows to display more columns

    import pandas as pd
    pd.options.display.max_columns = 10
    

    (this allows 10 columns to display, you can change this as you need)

  • Like that you can change the number of rows as you need to display as follows to display more rows

    pd.options.display.max_rows = 999
    

    (this allows to print 999 rows at a time)

this should works fine

Please kindly refer the doc to change more options/settings for pandas

Amila Viraj
  • 994
  • 10
  • 9
7

I have created a small utility function, this works well for me

def display_text_max_col_width(df, width):
    with pd.option_context('display.max_colwidth', width):
        print(df)

display_text_max_col_width(train_df["Description"], 800)

I can change length of the width as per my requirement, without setting any option permanently.

Sachin Rastogi
  • 409
  • 5
  • 8
6

If you're using jupyter notebook, you can also print pandas dataframe as HTML table, which will print full strings.

from IPython.display import display, HTML
display(HTML(df.to_html()))

Output

    one
0   one
1   two
2   This is very long string very long string very long string veryvery long string
kHarshit
  • 11,362
  • 10
  • 52
  • 71
4

Is this what you meant to do ?

In [7]: x =  pd.DataFrame({'one' : ['one', 'two', 'This is very long string very long string very long string veryvery long string']})

In [8]: x
Out[8]: 
                                                 one
0                                                one
1                                                two
2  This is very long string very long string very...

In [9]: x['one'][2]
Out[9]: 'This is very long string very long string very long string veryvery long string'
fixxxer
  • 15,568
  • 15
  • 58
  • 76
3

The way I often deal with the situation you describe is to use the .to_csv() method and write to stdout:

import sys

df.to_csv(sys.stdout)

Update: it should now be possible to just use None instead of sys.stdout with similar effect!

This should dump the whole dataframe, including the entirety of any strings. You can use the to_csv parameters to configure column separators, whether the index is printed, etc. It will be less pretty than rendering it properly though.

I posted this originally in answer to the somewhat-related question at Output data from all columns in a dataframe in pandas

user2428107
  • 3,003
  • 3
  • 17
  • 19