458

I converted a Pandas dataframe to an HTML output using the DataFrame.to_html function. When I save this to a separate HTML file, the file shows truncated output.

For example, in my TEXT column,

df.head(1) will show

The film was an excellent effort...

instead of

The film was an excellent effort in deconstructing the complex social sentiments that prevailed during this period.

This rendition is fine in the case of a screen-friendly format of a massive Pandas dataframe, but I need an HTML file that will show complete tabular data contained in the dataframe, that is, something that will show the latter text element rather than the former text snippet.

How would I be able to show the complete, non-truncated text data for each element in my TEXT column in the HTML version of the information? I would imagine that the HTML table would have to display long cells to show the complete data, but as far as I understand, only column-width parameters can be passed into the DataFrame.to_html function.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Amy
  • 4,693
  • 3
  • 12
  • 7
  • See https://pandas.pydata.org/pandas-docs/stable/user_guide/options.html. – flow2k Nov 01 '20 at 01:52
  • Does this answer your question? [Pretty-print an entire Pandas Series / DataFrame](https://stackoverflow.com/questions/19124601/pretty-print-an-entire-pandas-series-dataframe) – theProcrastinator Mar 16 '22 at 07:58

11 Answers11

766

Set the display.max_colwidth option to None (or -1 before version 1.0):

pd.set_option('display.max_colwidth', None)

set_option documentation

For example, in IPython, we see that the information is truncated to 50 characters. Anything in excess is ellipsized:

Truncated result

If you set the display.max_colwidth option, the information will be displayed fully:

Non-truncated result

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
behzad.nouri
  • 74,723
  • 18
  • 126
  • 124
192
pd.set_option('display.max_columns', None)  

id (second argument) can fully show the columns.

rafaelvalle
  • 6,683
  • 3
  • 34
  • 36
user7579768
  • 1,937
  • 1
  • 8
  • 2
168

While pd.set_option('display.max_columns', None) sets the number of the maximum columns shown, the option pd.set_option('display.max_colwidth', -1) sets the maximum width of each single field.

For my purposes I wrote a small helper function to fully print huge data frames without affecting the rest of the code. It also reformats float numbers and sets the virtual display width. You may adopt it for your use cases.

def print_full(x):
    pd.set_option('display.max_rows', None)
    pd.set_option('display.max_columns', None)
    pd.set_option('display.width', 2000)
    pd.set_option('display.float_format', '{:20,.2f}'.format)
    pd.set_option('display.max_colwidth', None)
    print(x)
    pd.reset_option('display.max_rows')
    pd.reset_option('display.max_columns')
    pd.reset_option('display.width')
    pd.reset_option('display.float_format')
    pd.reset_option('display.max_colwidth')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Karl Adler
  • 15,780
  • 10
  • 70
  • 88
86

Jupyter Users

Whenever I need this for just one cell, I use this:

with pd.option_context('display.max_colwidth', None):
  display(df)
iamyojimbo
  • 4,233
  • 6
  • 32
  • 39
  • 4
    For multiple attributes: `with pd.option_context('display.max_colwidth', None, 'display.max_rows', None):` `display(df)` – prabhakar267 Oct 13 '22 at 14:30
  • The `option_context` works great in PyCharm too, in the Debugger console or in the Python console (of course with a `print` instead of `display`) – hlongmore Feb 06 '23 at 10:13
27

Try this too:

pd.set_option("max_columns", None) # show all cols
pd.set_option('max_colwidth', None) # show full width of showing cols
pd.set_option("expand_frame_repr", False) # print cols side by side as it's supposed to be
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
bitbang
  • 1,804
  • 14
  • 18
11

The following code results in the error below:

pd.set_option('display.max_colwidth', -1)

FutureWarning: Passing a negative integer is deprecated in version 1.0 and will not be supported in future version. Instead, use None to not limit the column width.

Instead, use:

pd.set_option('display.max_colwidth', None)

This accomplishes the task and complies with versions of Pandas following version 1.0.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Colonel_Old
  • 852
  • 9
  • 15
11

Display the full dataframe for a specific cell:

import pandas as pd
with pd.option_context('display.max_colwidth', None,
                       'display.max_columns', None,
                       'display.max_rows', None):
    display(df)

The method above can be extended with more options.

Updated helper function from Karl Adler:

def display_full(x):
    with pd.option_context('display.max_rows', None,
                           'display.max_columns', None,
                           'display.width', 2000,
                           'display.float_format', '{:20,.2f}'.format,
                           'display.max_colwidth', None):
        display(x)

Change display options for all cells:

pd.set_option('display.max_colwidth', None)
pd.set_option('display.max_rows', None)
pd.set_option('display.max_columns', None)
display(df)
Benjamin Ziepert
  • 1,345
  • 1
  • 15
  • 19
10

Another way of viewing the full content of the cells in a Pandas dataframe is to use IPython's display functions:

from IPython.display import HTML

HTML(df.to_html())
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
joelostblom
  • 43,590
  • 17
  • 150
  • 159
5

For those looking to do this in Dask:

I could not find a similar option in Dask, but if I simply do this in same notebook for Pandas it works for Dask too.

import pandas as pd
import dask.dataframe as dd
pd.set_option('display.max_colwidth', -1) # This will set the no truncate for Pandas as well as for Dask. I am not sure how it does for Dask though, but it works.

train_data = dd.read_csv('./data/train.csv')
train_data.head(5)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prabhat
  • 4,066
  • 4
  • 34
  • 41
1

For those who like to reduce typing (i.e., everyone!): pd.set_option('max_colwidth', None) does the same thing

Apostolos
  • 3,115
  • 25
  • 28
0

I would like to offer other methods. If you don't want to always set it as default.

# First method
list(df.itertuples()) # This would force pandas to explicitly display your dataframe, however it's not that beautiful

# Second method
import tabulate
print(tabulate(df, tablefmt='psql', headers='keys')) 
# `headers` are your columns, `keys` are the current columns
# `psql` is one type of format for tabulate to organize before, you could pick other format you like in the documentation
himra
  • 1
  • 1