0

Let's say I have a dataframe as shown below. I need to add a new url column to the dataframe and then create a clickable url link by using the values of the other associated rows.

import pandas as pd
url='https://stackoverflow.com/'
df = pd.DataFrame({'A': ('62392411', 60273469), 'B': ('questions/', 'questions/')})
>>> df
          A           B
0  62392411  questions/
1  60273469  questions/

I am looking to get something like this:

>>> df
          A           B         url    
0  62392411  questions/         https://stackoverflow.com/questions/62392411
1  60273469  questions/         https://stackoverflow.com/questions/60273469
Alex Man
  • 457
  • 4
  • 19
  • 2
    `df.assign(Url=[f"https://stackoverflow.com/{b}{a}" for a,b in zip(df['A'],df['B'])]).style.format(make_clickable,subset=['Url'])` function taken from [here](https://stackoverflow.com/questions/42263946/how-to-create-a-table-with-clickable-hyperlink-in-pandas-jupyter-notebook) does this help? – anky Jun 15 '20 at 17:19

1 Answers1

1

If both columns are strings and won't contain NaN, then you could do something like:

df['url'] = 'somestring/' + df['B'].astype(str) + df['A'].astype(str)

Output:

0    somestring/questions/62392411
1    somestring/questions/60273469
Name: url, dtype: object

To make it clickable something like this should work:

def make_clickable(val):
    return '<a href="{}">{}</a>'.format(val,val)

df.style.format({'url': make_clickable})