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…
- 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
- Assistant
Absolutely! You can display clickable URLs in a Jupyter Notebook using Pandas'
to_htmlmethod with a custom formatter, or by using theIPython.displaymodule. Here’s a simple and effective way to do it:Suppose your DataFrame is called
dfand has columnstitleandurl.Method 1: Using
IPython.display.displayandto_htmlimport 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=Falseis 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!
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.