Writing full contents of Pandas dataframe to HTML table

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:


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:




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)

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Writing full contents of Pandas dataframe to HTML table