Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas html: Don't truncate long values

Tags:

python

pandas

I understand that pandas does cut-off long elements. However, why does it do that in the html output?

import pandas as pd
df = pd.DataFrame(columns=['url'], index=[0])
df['url'] = 'd12dn1928d1n298dn18d9n219d8n18n118219d8n21e12903e21kj9012j9301j2391023j209d12dn1928d1n298dn18d9n219d8n18n118219d8n21e12903e21kj9012j9301j2391023j209d12dn1928d1n298dn18d9n219d8n18n118219d8n21e12903e21kj9012j9301j2391023j209d12dn1928d1n298dn18d9n219d8n18n118219d8n21e12903e21kj9012j9301j2391023j209d12dn1928d1n298dn18d9n219d8n18n118219d8n21e12903e21kj9012j9301j2391023j209'
In [2]: df
Out[2]: 
                                                 url
0  d12dn1928d1n298dn18d9n219d8n18n118219d8n21e129...

In [3]: df.to_html()
Out[3]: u'<table border="1" class="dataframe">\n  <thead>\n    <tr style="text-align: right;">\n      <th></th>\n      <th>url</th>\n    </tr>\n  </thead>\n  <tbody>\n    <tr>\n      <th>0</th>\n      <td>d12dn1928d1n298dn18d9n219d8n18n118219d8n21e129...</td>\n    </tr>\n  </tbody>\n</table>'

even in the html output (where it is obvious that it won't fit the screen width), the column value is truncated. How can I force pandas to not truncate, both with and without html?

like image 379
FooBar Avatar asked Mar 08 '16 09:03

FooBar


People also ask

How do I get rid of whitespaces in pandas?

Pandas provide predefine method “pandas. Series. str. strip()” to remove the whitespace from the string.

How do you drop the last 5 rows in pandas?

Drop Last Row of Pandas DataFrame Using iloc[] iloc[] you can drop the rows from DataFrame and use -1 to drop the last row. For example use df. iloc[:-1,:] to select all rows except the last one and then assign it back to the original variable which ideally drops the last row from DataFrame.

Is pandas query faster than LOC?

The query function seams more efficient than the loc function. DF2: 2K records x 6 columns. The loc function seams much more efficient than the query function.


1 Answers

You can turn off the truncating display option with:

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

or before Pandas 1.0,

pd.set_option('display.max_colwidth', -1)
like image 186
YOBA Avatar answered Sep 28 '22 10:09

YOBA