374

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,030
  • 21
  • 100
  • 124
Amy
  • 3,853
  • 3
  • 10
  • 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

9 Answers9

656

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,030
  • 21
  • 100
  • 124
behzad.nouri
  • 69,003
  • 18
  • 120
  • 118
  • 31
    According to the docs you should set it to `None` to mean unlimited. – kynan Nov 06 '15 at 15:16
  • 2
    That's what I'm talking about! – Ivan Aug 25 '17 at 13:47
  • 4
    The `max_columns` answer worked for me, which uses `None` as the second argument of `set_option`. – kilojoules Jun 15 '18 at 19:43
  • 25
    If you want these display options to apply just once, and not permanently, you can also use the context manager as such: `with pd.option_context('display.max_colwidth', -1): display(df)` – S.A. Nov 06 '18 at 10:01
  • I used `None` and got `ValueEror`. Thank you @xpt @S.A. for clarifying – Verma Aman Jul 17 '19 at 17:13
  • 7
    With the API v3, when I set `display.max_colwidth` to `-1` I got a `FutureWarning`. Replacing the `-1` with `None` worked, and eliminated the warning. – jazcap53 Jul 03 '20 at 12:27
  • In case you want to set back to a different value you can fill an integer number instead of -1, I used 50 in my case: pd.set_option('display.max_colwidth', 50) – Fouad Djebbar Sep 16 '20 at 15:43
  • Is there any way to adjust the row height? For example, if we have 2 really long texts and we'd like to view them side by side in the dataframe. Can't seem to find any pandas options for this. – piedpiper Nov 10 '21 at 02:54
174
pd.set_option('display.max_columns', None)  

id (second argument) can fully show the columns.

rafaelvalle
  • 6,173
  • 3
  • 32
  • 34
user7579768
  • 1,757
  • 1
  • 7
  • 2
  • Great little aside, the number of columns were being truncated for me, as represented by and ellipsis (...) near the middle of my table. Thanks! – four43 Jan 01 '18 at 18:30
  • 7
    This worked for me and not the other answer. I'm using Python 3.6 – Durga Swaroop Jun 01 '18 at 19:11
  • 1
    This didn't work for me on Python 2.7, but the first answer by @behzad.nouri did. – rer Jul 11 '18 at 18:06
  • 2
    This should be the accepted answer. The current accept answer with option `max_colwidth` can solve the truncate issue caused by a field has too long values. However, I think the truncate issues for most people is actually too many columns. So, this `max_columns` should be the accepted one. – Christopher May 23 '19 at 23:52
  • @Christopher, OP asked about width of one column, many come here for both so answer by Karl is more relevant for them. – Alexei Martianov Dec 12 '19 at 04:52
  • This worked for me. However, I have a question: how do you go back to the default value? – desmond13 Nov 18 '20 at 08:58
  • I found how to do it: `pd.reset_option('display.max_rows')` or `pd.reset_option('display.max_columns')` – desmond13 Nov 18 '20 at 09:00
  • Does someone know how to reset this? tried replacing `None` with 5, 10 like values, but didn't work. – Pavindu May 26 '21 at 14:39
133

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,030
  • 21
  • 100
  • 124
Karl Adler
  • 14,019
  • 10
  • 62
  • 86
52

Jupyter Users

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

with pd.option_context('display.max_colwidth', None):
  display(df)
iamyojimbo
  • 3,645
  • 5
  • 28
  • 36
  • 6
    This is the most common use case and cleanest way to represent data. Otherwise, it'll be chaotic to have it enabled everywhere – Adib Apr 14 '21 at 07:40
  • To add to the comment by @Adib: Globally changing it to `None` can also lead to performance issues in Notebooks. – Sammy Nov 08 '21 at 12:32
19

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,030
  • 21
  • 100
  • 124
bitbang
  • 1,235
  • 10
  • 14
  • 1
    Use `pd.set_option("display.max_columns", None)` when you meet `Pattern matched multiple keys` error. – Kxrr May 13 '22 at 03:49
8

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,030
  • 21
  • 100
  • 124
Colonel_Old
  • 724
  • 8
  • 13
7

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,030
  • 21
  • 100
  • 124
joelostblom
  • 35,621
  • 16
  • 134
  • 143
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,030
  • 21
  • 100
  • 124
Prabhat
  • 3,669
  • 3
  • 31
  • 38
0

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

Apostolos
  • 2,841
  • 20
  • 25