0

I have a dataframe as given below [input_dataframe]. I want the column Number to display as a clickable hyperlink. For e.g., under column Number, I want it to be displayed as 1234, but it should be as hyperlink and the link should be taken from the next column.

I tried the following code, but it is showing the entire hyperlink from the 2nd column twice in the first column [Output_dataframe]. Kindly help me in resolving this issue.

Code:

import pandas as pd
df = pd.read_excel("C:\\Users\\XYZ\\Desktop\\Test_1.xlsx",sheet_name='Sheet1')
df['Number'] = '<a href=' + df['url'] + '><div>' + df['url'] + '</div></a>'
df.to_excel("Test_2.xlsx",index=False)

input_dataframe

Output_dataframe

Timeless
  • 22,580
  • 4
  • 12
  • 30
Bharath
  • 11
  • 4
  • This works!!! def make_clickable(Number, url): return '{}'.format(Number,url) df['Number'] = df.apply(lambda x: make_clickable(x['url'], x['Number']), axis=1) df.style a = df.to_html(index=False,render_links=True, escape=False) – Bharath Oct 03 '22 at 07:22

1 Answers1

0

Can't test but believe this should work:

df["Number"] = df["url"].apply(lambda x: f"=HYPERLINK({x}, {x})")
Jason Baker
  • 3,170
  • 2
  • 12
  • 15