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
1
  • 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 does this help? Commented Jun 15, 2020 at 17:19

1 Answer 1

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})
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I am not sure if that can make it clickable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.