将 Pandas 数据框的全部内容写入 HTML 表格
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25070758/
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
Writing full contents of Pandas dataframe to HTML table
提问by lmart999
I am embedding links in one column of a Pandas dataframe (table, below) and writing the dataframe to hmtl.
我将链接嵌入到 Pandas 数据框(下表)的一列中,并将数据框写入 hmtl。
Links in the dataframe table are formatted as shown (indexing first link in table):
数据帧表中的链接格式如下(索引表中的第一个链接):
In: table.loc[0,'Links']
Out: u'<a href="http://xxx.xx.xxx.xxx/browser/I6.html">I6</a>'
If I view (rather than index a specific row) the dataframe (in notebook), the link text is truncated:
如果我查看(而不是索引特定行)数据框(在笔记本中),链接文本将被截断:
<a href="http://xxx.xx.xxx.xxx/browser/I6.html...
I write the dataframe to html:
我将数据框写入 html:
table_1=table.to_html(classes='table',index=False,escape=False)
But, the truncated link (rather than the full text) is written to the html table:
但是,截断的链接(而不是全文)被写入 html 表:
<td> <a href="http://xxx.xx.xxx.xxx/browser/I6.html...</td>\n
I probably need an additional parameter for to_html().
我可能需要一个额外的 to_html() 参数。
Look at the documentation now, but advice appreciated:
现在查看文档,但建议赞赏:
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_html.html
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_html.html
Thanks!
谢谢!
采纳答案by user1893148
So there is probably a pandas-specific explanation, but you could also work around the problem by (a) replacing the links with a key value, (b) writing the html table string, and then (c) replacing the keys with the appropriate links.
所以可能有一个特定于Pandas的解释,但您也可以通过 (a) 用键值替换链接,(b) 编写 html 表字符串,然后 (c) 用适当的键替换键来解决这个问题链接。
For example, replace each link with a key, storing the keys in a dict:
例如,用一个键替换每个链接,将键存储在一个字典中:
map = {}
for i in df.index:
counter = 0
if df.ix[i]['Links'] in map:
df.ix[i, 'Links'] = map[df.ix[i]['Links']]
else:
map[df.ix[i, 'Links']] = 'href' + str(counter)
counter += 1
df.ix[i, 'Links'] = map[df.ix[i]['Links']]
Write the table:
写表:
table_1 = df.to_html(classes='table',index=False,escape=False)
Re-write the links:
重新编写链接:
for key, value in map.iteritems():
table_1 = table_1.replace(value, key)

