本文介绍了Pandas to_html() 截断字符串内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含文本数据的 Python Pandas DataFrame 对象.我的问题是,当我使用 to_html() 函数,它截断输出中的字符串.

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.

例如:

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

输出在 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>

SO 上有一个相关问题,但它使用占位符和搜索/替换功能对 HTML 进行后处理,我想避免这种情况:

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.

推荐答案

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

What you are seeing is pandas truncating the output for display purposes only.

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

The default max_colwidth value is 50 which is what you are seeing.

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

You can set this value to whatever you desire or you can set it to -1 which effectively turns this off:

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

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

Although I would advise against this, it would be better to set it to something that can be displayed easily in your console or ipython.

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

A list of the options can be found here: http://pandas.pydata.org/pandas-docs/stable/options.html

这篇关于Pandas to_html() 截断字符串内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 10:48