Python Pandas to_html() 截断字符串内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/26277757/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas to_html() truncates string contents
提问by Timo
I have a Python Pandas DataFrameobject containing textual data. My problem is, that when I use to_html()function, it truncates the strings in the output.
我有一个DataFrame包含文本数据的 Python Pandas对象。我的问题是,当我使用to_html()函数时,它会截断输出中的字符串。
For example:
例如:
import pandas
df = pandas.DataFrame({'text': ['Lorem ipsum dolor sit amet, consectetur adipiscing elit.']})
print (df.to_html())
The output is truncated at adapis...
输出被截断为 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>
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:
SO 上有一个相关问题,但它使用占位符和搜索/替换功能对 HTML 进行后处理,我想避免这种情况:
Is there a simpler solution to this problem? I could not find anything related from the documentation.
采纳答案by EdChum
What you are seeing is pandas truncating the output for display purposes only.
您所看到的是熊猫仅出于显示目的而截断输出。
The default max_colwidthvalue is 50 which is what you are seeing.
默认max_colwidth值为 50,这就是您所看到的。
You can set this value to whatever you desire or you can set it to -1 which effectively turns this off:
您可以将此值设置为您想要的任何值,也可以将其设置为 -1 以有效地关闭此功能:
pd.set_option('display.max_colwidth', -1)
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.
尽管我建议不要这样做,但最好将其设置为可以在您的控制台或 ipython 中轻松显示的内容。
A list of the options can be found here: http://pandas.pydata.org/pandas-docs/stable/options.html
可以在此处找到选项列表:http: //pandas.pydata.org/pandas-docs/stable/options.html
回答by Boris Gorelik
it seems that pd.set_option('display.max_colwidth', -1)is indeed the only option. To prevent irreversible global changes of how dataframes are presented in the console, you may save the previous setting in a variable and restore it immediately after the usage, as follows:
看来这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)

![Python OSError: [Errno 13] 更新 setuptools 时权限被拒绝](/res/img/loading.gif)