Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Insert a link inside a Pandas table

I'd like to insert a link (to a web page) inside a Pandas table, so when it is displayed in an IPython notebook, I could press the link.

I tried the following:

In [1]: import pandas as pd  In [2]: df = pd.DataFrame(range(5), columns=['a'])  In [3]: df['b'] = df['a'].apply(lambda x: 'http://example.com/{0}'.format(x))  In [4]: df Out[4]:    a                     b 0  0  http://example.com/0 1  1  http://example.com/1 2  2  http://example.com/2 3  3  http://example.com/3 4  4  http://example.com/4 

But the URL is just displayed as text.

I also tried using an IPython HTML object:

In [5]: from IPython.display import HTML  In [6]: df['b'] = df['a'].apply(lambda x:HTML('http://example.com/{0}'.format(x)))  In [7]: df Out[7]:    a                                                 b 0  0  <IPython.core.display.HTML object at 0x0481E530> 1  1  <IPython.core.display.HTML object at 0x0481E770> 2  2  <IPython.core.display.HTML object at 0x0481E7B0> 3  3  <IPython.core.display.HTML object at 0x0481E810> 4  4  <IPython.core.display.HTML object at 0x0481EA70> 

But it will only display the repr of the object.

Any other ideas?


alko got the right answer. I just wanted to add that the cell width is limited by default, and long HTML code will be truncated, i.e.:

<a href="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0">xxx</a> 

will become this:

<a href="aaaaaaaaaaaaaaaaaaaaaa... 

and won't be displayed correctly. (Even though the text xxx is short and can fit in the cell.)

I've bypassed it by setting:

pd.set_printoptions(max_colwidth=-1) 
like image 866
lev Avatar asked Nov 17 '13 20:11

lev


People also ask

How do I make a link clickable in Python?

link() method in Python is used to create a hard link. This method creates a hard link pointing to the source named destination.

How do I join strings in pandas?

Python | Pandas Series.str.cat() to concatenate string Pandas str.cat() is used to concatenate strings to the passed caller series of string. Distinct values from a different series can be passed but the length of both the series has to be same. .

What does .ADD do in pandas?

Pandas DataFrame add() Method The add() method adds each value in the DataFrame with a specified value. The specified value must be an object that can be added to the values of the DataFrame.

How to insert a column in pandas Dataframe?

Pandas insert method allows the user to insert a column in a dataframe or series (1-D Data frame). A column can also be inserted manually in a data frame by the following method, but there isn’t much freedom here. For example, even column location can’t be decided and hence the inserted column is always inserted in the last position.

How to create clickable link from single column in pandas Dataframe?

(1) Pandas method: to_html and parameter render_links=True: (2) Create clickable link from single column in Pandas DataFrame: In the next section, We'll review the steps to apply the above examples in practice. The first example to cover will create links from all columns which contain valid URL-s with option - render_links.

How to create a new column with a short hyperlink in pandas?

from two columns of Pandas DataFrame to a new column with a short hyperlink. Let's say that our DataFrame has two values - name and url . To create a new column which is a shortened hyperlink we can make a function. Then we can use method format of style in order to apply the function to different columns - given as a dict:

How to create a hyperlink in pandas Dataframe and JupyterLab?

Here are few different approaches to create a hyperlink in Pandas DataFrame and JupyterLab: (1) Pandas method: to_html and parameter render_links=True: (2) Create clickable link from single column in Pandas DataFrame: In the next section, We'll review the steps to apply the above examples in practice.


1 Answers

I suppose you have to represent whole Pandas object as an HTML object, that is

In [1]: from IPython.display import HTML  In [2]: df = pd.DataFrame(list(range(5)), columns=['a'])  In [3]: df['a'] = df['a'].apply(lambda x: '<a href="http://example.com/{0}">link</a>'.format(x))  In [4]: HTML(df.to_html(escape=False)) 

Sorry, now I don't have IPython at hand, and can't check whether the output is correct.

like image 137
alko Avatar answered Oct 01 '22 06:10

alko