0

I have a pandas dataframe that looks like:

        heading1  heading2
 issue1 data        data
 issue2 data        data

enter image description here

And I am converting this to excel using df.to_excel function. Now I want to convert the index to clickable hyperlinks. For example - If i click Issue1, it should take me to www.issue1.com. I know how to convert pandas dataframe column to hyperlink, but not sure if I can make the indices clickable. Any help would be appreciated.

mybrave
  • 1,662
  • 3
  • 20
  • 37
  • https://stackoverflow.com/questions/42263946/how-to-create-a-table-with-clickable-hyperlink-in-pandas-jupyter-notebook hope this helps – Madhur Yadav Mar 23 '20 at 13:19
  • The thread in the above link tells how to make column values as hyperlink but I want the index values as hyperlink. – Aditya Garg Mar 24 '20 at 04:30

1 Answers1

0

Use pandas.DataFrame.set_index along with pandas.DataFrame.index.

import pandas as pd

df = pd.DataFrame(
    dict(
        MUL1=["Data", "Data"],
        MUL2=["Data", "Data"],
    ),
    index=["Issue1", "Issue2"],
)

df = df.set_index("www." + df.index + ".com")
print(df)

edit: OP commented:

This just changes the index from 'issue1' to 'www.issue1.com' but does not make it clickable. (=HYPERLINK) – Aditya Garg

My response is that the clickabilty of the link depends on what program you are displaying the link in. I use Pycharm, which is able to detect URLs in console output and makes them clickable by default: enter image description here

That being said it may not be clickable when running the script in other programs like cmd.exe for example. Making it into an html or markdown style link is not going to change this for things like cmd. However, if you're going to display this DataFrame in something that does support a particular link formatting method, then you can try some of the following examples:

import pandas as pd

df = pd.DataFrame(
    dict(
        MUL1=["Data", "Data"],
        MUL2=["Data", "Data"],
    ),
    index=["Issue1", "Issue2"],

)
index = df.index

# Basic link example
df = df.set_index('www.' + index + '.com/')
print(df)

# Markdown Link Example
df = df.set_index("[" + index + "](www." + index + ".com)")
print(df)

# angle bracket format example
df = df.set_index("<www." + index + ".com>")
print(df)

# html link example
df = df.set_index('<a href="www.' + index + '.com">' + index + '</a>')
print(df)

Phillyclause89
  • 674
  • 4
  • 12