75501

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:

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!

Answer1:

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)

Recommend

  • Dummy Variables in Julia
  • validate mustache tag value from json file
  • Python Conditionally Add Class to Tags in HTML Table
  • Android Menu Group Checkable
  • How to use lambda to apply style to Pandas DataFrame
  • How to change the type of a function reference?
  • Grouping based on the node value if only it exists using XSLT 1.0
  • displaying markdown in my textarea
  • Can't run Appium tests on iOS 10 on real device
  • OAuth and the YouTube API
  • Adding directive inside the directive programatically
  • How to pass nginx proxy url for socket
  • what makes a request a new request in asp.net C#
  • System.InvalidCastException: Specified cast is not valid
  • Meteor helpers not available in Angular template
  • Read a local file using javascript
  • PHPUnit_Framework_TestCase class is not available. Fix… - Makegood , Eclipse
  • Apache 2.4 and php-fpm does not trigger apache http basic auth for php pages
  • jquery mobile loadPage not working
  • Rearranging Cells in UITableView Bug & Saving Changes
  • Unanticipated behavior
  • Cannot Parse HTML Data Using Android / JSOUP
  • Matrix multiplication with MKL
  • using conditional logic : check if record exists; if it does, update it, if not, create it
  • How to get icons for entities from eclipse?
  • Proper way to use connect-multiparty with express.js?
  • Load html files in TinyMce
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • What are the advantages and disadvantages of reading an entire file into a single String as opposed
  • Getting Messege Twice Using IMvxMessenger
  • Can't mass-assign protected attributes when import data from csv file
  • JaxB to read class hierarchy
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • Converting MP3 duration time
  • Conditional In-Line CSS for IE and Others?
  • How to load view controller without button in storyboard?