我有一个数据帧,其中一列都是超链接(例如,http://example.com(。我已经设法使这些链接在Jupyter Notebook中呈现为html:
from IPython.display import HTML
df["URL"] = df["URL"].apply(lambda x: '<a href="{}">{}</a>'.format(x,x))
df = HTML(df.to_html(escape=False))
df
但是我无法将这些链接以 html 而不是文本的形式写入 Excel(或 ODS(电子表格。在上面,df
成为IPython.core.display.HTML
对象,没有熊猫的to_excel()
。
当我将单个单元格转换为lambda x: HTML('<a href="{}">{}</a>'.format(x,x))
IPython.core.display.HTML
,然后在数据帧上调用to_excel
时,出现异常:
Unexpected data type <class 'IPython.core.display.HTML'>
您可以使用熊猫内置的超链接功能
import pandas as pd
df = pd.DataFrame({'link':['=HYPERLINK("http://www.someurl.com", "some website")']})
df.to_excel('mohammad-rocks.xlsx')