使用 pandas dataframe.to_html() 时重新格式化列宽
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31550889/
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
Reformatting column width when using pandas dataframe.to_html()
提问by kdubs
I have a pandas dataframe that I want to display as a table in an html template. It works for the most part, but some of the strings in the dataframe are being truncated. I am using to_html()to convert my pandas dataframe to an html friendly table and tried using the col_spaceargument but it didn't seem to have any affect.
我有一个 Pandas 数据框,我想在 html 模板中显示为表格。它在大多数情况下都有效,但数据框中的某些字符串被截断。我正在使用to_html()将我的 Pandas 数据帧转换为一个 html 友好的表并尝试使用该col_space参数,但它似乎没有任何影响。
The python code:
蟒蛇代码:
options = dbi.getDBTables() #returns a list of the names of available tables
table_headers = []
for table_name in options:
table_name = str(table_name)
df = dbi.selectDF("SELECT * FROM %s LIMIT 1" % table_name) #gets the actual dataframe for each table name
header = df.columns.values
header = "<br>".join(header)
table_headers.append(header)
header_dict = dict(zip(options,table_headers))
table_options = pandas.DataFrame(header_dict,index=[0])
table_options = table_options.to_html(classes=["table table-hover"],index=False,escape=False,col_space=400) #changing the col_space does nothing.
search_dict = {'table_names':table_options}
The html code:
html代码:
<div class="table-responsive">
{{ table_names | safe }}
</div>
Picture of table with truncation occurring:

发生截断的表格图片:

How do you get pandas to stop truncating (shown as '...' in picture) content?
你如何让Pandas停止截断(在图片中显示为“...”)内容?
Much appreciated!
非常感激!
回答by kdubs
Just needed to set pandas.set_option('display.max_colwidth',100)
只需要设置 pandas.set_option('display.max_colwidth',100)

