将 pandas 数据框的全部内容写入HTML表格 [英] Writing full contents of Pandas dataframe to HTML table
问题描述
我将链接嵌入熊猫数据框(下表中的一列)中,并将数据框写入hmtl。
dataframe表中的链接格式如图所示(索引表中的第一个链接):
In:table.loc [0,'Links']
$ p $如果我查看(而不是索引特定行)数据框(在笔记本中),链接文本被截断:
输出: u'< a href =http://xxx.xx.xxx.xxx/browser/I6.html> I6< / a>'
pre>< a href =http://xxx.xx.xxx.xxx/browser/I6.html ...
我将数据框写入html:
table_1 = table .to_html(classes ='table',index = False,escape = False)
但是,截断链接(而不是全文)被写入到html表格:
< td>< a href =http ://xxx.xx.xxx.xxx/browser/I6.html...< / td> \\\
我可能需要一个额外的参数to_html()。
现在查看文档,但建议表示赞赏:
< a href =http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_html.html =nofollow> http://pandas.pydata.org/pandas-docs/ dev / generated / pandas.DataFrame.to_html.html
谢谢!
例如,用键替换每个链接,将键存储在字典中:
map = {}
for df.index:
counter = 0
if df.ix [i] ['Links'] in map:
df.ix [i,'Links'] =地图[df.ix [i] ['Links']]
其他:
地图[df.ix [i,'Links']] = 'href'+ str(counter)
counter + = 1
df.ix [i,'Links'] = map [df.ix [i] ['Links']]
写下表:
table_1 = df.to_html(classes ='table',index = False,escape = False)
重新编写链接:
为key,map.iteritems()中的值:
table_1 = table_1。替换(值,键)
I am embedding links in one column of a Pandas dataframe (table, below) and writing the dataframe to 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:
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:
<td> <a href="http://xxx.xx.xxx.xxx/browser/I6.html...</td>\n
I probably need an additional parameter for to_html().
Look at the documentation now, but advice appreciated:
http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_html.html
Thanks!
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.
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)
这篇关于将 pandas 数据框的全部内容写入HTML表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!