Pandas to_html() truncates string contents

Tim*_*imo 71 html python pandas

I have a Python Pandas DataFrame object containing textual data. My problem is, that when I use to_html() function, it truncates the strings in the output.

For example:

import pandas
df = pandas.DataFrame({'text': ['Lorem ipsum dolor sit amet, consectetur adipiscing elit.']})
print (df.to_html())
Run Code Online (Sandbox Code Playgroud)

The output is truncated at adapis...

<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>text</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td> Lorem ipsum dolor sit amet, consectetur adipis...</td>
    </tr>
  </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

There is a related question on SO, but it uses placeholders and search/replace functionality to postprocess the HTML, which I would like to avoid:

Is there a simpler solution to this problem? I could not find anything related from the documentation.

EdC*_*ica 97

您看到的是pandas截断输出仅用于显示目的.

默认max_colwidth值为50,这是您所看到的.

您可以将此值设置为您想要的任何值,也可以将其设置为-1,从而有效地将其关闭:

pd.set_option('display.max_colwidth', -1)
Run Code Online (Sandbox Code Playgroud)

虽然我建议不要这样做,但最好将它设置为可以在控制台或ipython中轻松显示的内容.

可以在此处找到选项列表:http://pandas.pydata.org/pandas-docs/stable/options.html

  • 在df.to_html()中的`"..."`计算为'True`.看起来好像`pandas`正在截断输出值,而不仅仅是显示. (7认同)

Bor*_*lik 18

这似乎pd.set_option('display.max_colwidth', -1)确实是唯一的选择.为了防止在控制台中显示数据框的方式发生不可逆转的全局更改,您可以将以前的设置保存在变量中,并在使用后立即恢复,如下所示:

    old_width = pd.get_option('display.max_colwidth')
    pd.set_option('display.max_colwidth', -1)
    open('some_file.html', 'w').write(some_data.to_html())
    pd.set_option('display.max_colwidth', old_width)
Run Code Online (Sandbox Code Playgroud)

  • 有一个更简单的解决方案如何设置临时选项:`with pd.option_context('display.max_colwidth', - 1):output_html = df.to_html()` (36认同)