0

I have URL column for each record in pandas DataFrame.

Name             CRMLinks                             NamedLink
Clint Eastwood   HTTP://example.com/link1/100e1e         Clint
Iron Side        HTTP://example.com/link2/202e1e         Iron

I want to convert CRMLinks and NamedLink column to clickable links to get to the URL that is shown in CRMLinks.

Here is what I have

base_url= 'https://example.com/'

df['CRM'] = base_url + df['CRM']

def make_clickable(link):
    # target _blank to open new window
    # extract clickable text to display for your link
    text = link.split('=')[1]
    return f'<a target="_blank" href="{link}">{text}</a>'

# link is the column with hyperlinks

df['CRM'] = df['CRM'].apply(make_clickable)

But I only get this in the df['CRM'] cell.

<a target="_blank" href="HTTP://example.com/link1">100e1e</a>

Thanks

2 Answers 2

1

I guess you need to use:

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

This will make those clickable


base_url= 'https://example.com/'

df['CRM'] = base_url + df['NamedLink']

def make_clickable(link):
    print(link)
    # target _blank to open new window
    # extract clickable text to display for your link
    text = link.split('=')[0]
    return f'<a target="_blank" href="{link}">{text}</a>'

# link is the column with hyperlinks
df.style.format({'CRM': make_clickable})

    Name            CRMLinks                         NamedLink  CRM
0   Clint_Eastwood  HTTP://example.com/link1/100e1e  Clint  https://example.com/Clint
1   Iron_Side       HTTP://example.com/link2/202e1e  Iron   https://example.com/Iron
Sign up to request clarification or add additional context in comments.

8 Comments

If I do that I : df['CRM'] = df.style.format({'CRM': make_clickable}) now get this in each row <pandas.io.formats.style.Styler object at 0x7f9db25015f8>
Check my answer. Updated one
ok now I get the clean URL in jupyter notebook. Should that not clickable? Am I wrong in expecting that the link should be clickable in jupyter notebook. This is what I get now in the dataframe: HTTP://example.com/link1/100e1e
It will be clickable only since I have used style.format
But it is not clickable since when I click on the link in the data frame within Jupiter notebook, it does nothing. Are you testing it in jupyter notebook
|
0

I'm guessing this line is the error. Replace your line :

return f'<a target="_blank" href="{link}">{text}</a>'

TO this n try:

return f'<a target="_blank" href="{link}/{text}">{text}</a>'

1 Comment

I get the same target blank.... <a target="_blank" href="HTTP://example.com/link1">100e1e</a>. Please note the base_url in my code. That may help

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.