1

I got 3 columns in my dataframe.

Example:

name = ['iphone']
price = ['1000']
url = ['https://iphone.com/']
MY_DF = pd.DataFrame({'name': name, 'price':price})
MY_DF.to_excel('123.xlsx')

I need did it in loop in my dataframe.

How to apply url as hyperlink to price? and export it to xlsx result need to be like:

enter image description here

The methods described in this article: add hyperlink to excel sheet created by pandas dataframe to_excel method do not suit me.

In my situation every url is unique and I don't know how to right apply them for my 'price'.

Seems like this could work, but I don't know how to loop it:

enter image description here

Thank you in advance

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • Does this answer your question? [add hyperlink to excel sheet created by pandas dataframe to\_excel method](https://stackoverflow.com/questions/31820069/add-hyperlink-to-excel-sheet-created-by-pandas-dataframe-to-excel-method) – Rodalm Oct 30 '21 at 12:56
  • @HarryPlotter I don't think so – Yaroslav Butorin Oct 30 '21 at 12:57
  • Please update your post with a sample of the DataFrame. It's not clear from the example how it's formated. Do you have 3 separate columns: product name, price and url? – Rodalm Oct 30 '21 at 17:16
  • @HarryPlotter i was find solution, see below. anyway thanks for your attention. – Yaroslav Butorin Oct 30 '21 at 18:12

1 Answers1

1
def make_hyperlink(value):
    url = "{}"
    return '=HYPERLINK("%s", "%s")' % (url.format(value).split(' ')[0], url.format(value).split(' ')[1])

Il combine my url and price as prices = 'urls' +' '+'price' append it to main dataframe as ['price'] and apply next myDF['price'] = myDF['price'].apply(lambda x: make_hyperlink(x)) and it works! =)