Skip to content
The Chat Archive
gpt-4.1-2025-04-14· OpenAI LMArena Human Preference 140K

I have a pandas dataframe with two columns (title and url). can you please write code…

  1. User

    I have a pandas dataframe with two columns (title and url). can you please write code that allows me to see it in jupyter notebook and be able to click on the url so that it opens in my browser

  2. Assistant

    Absolutely! You can display clickable URLs in a Jupyter Notebook using Pandas' to_html method with a custom formatter, or by using the IPython.display module. Here’s a simple and effective way to do it:

    Suppose your DataFrame is called df and has columns title and url.

    Method 1: Using IPython.display.display and to_html

    import pandas as pd
    from IPython.display import display, HTML
    
    # Sample DataFrame
    df = pd.DataFrame({
        'title': ['OpenAI', 'Pandas'],
        'url': ['https://openai.com', 'https://pandas.pydata.org']
    })
    
    # Create a new column with clickable links
    df['link'] = df.apply(lambda x: f'<a href="{x["url"]}" target="_blank">{x["title"]}</a>', axis=1)
    
    # Display only the clickable links
    display(HTML(df[['link']].to_html(escape=False, index=False)))
    

    Method 2: Format the URL column as clickable

    If you want to keep both columns visible:

    import pandas as pd
    from IPython.display import display, HTML
    
    # Sample DataFrame
    df = pd.DataFrame({
        'title': ['OpenAI', 'Pandas'],
        'url': ['https://openai.com', 'https://pandas.pydata.org']
    })
    
    # Make the url column clickable
    df['url'] = df['url'].apply(lambda x: f'<a href="{x}" target="_blank">{x}</a>')
    
    # Display the DataFrame with clickable URLs
    display(HTML(df.to_html(escape=False, index=False)))
    

    Note:

    • escape=False is important to render HTML.
    • target="_blank" makes the link open in a new tab.

    Let me know if you want to customize the appearance further!

Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.