我有一个数据框架,其中一列都是超链接(例如,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对象,没有了pandas的to_excel()。
当我使用lambda x: HTML('<a href="{}">{}</a>'.format(x,x))将单个单元格转换为IPython.core.display.HTML,然后在DataFrame上调用to_excel时,我得到了异常:
Unexpected data type <class 'IPython.core.display.HTML'>
发布于 2018-01-29 02:33:15
您可以使用pandas中内置的超链接函数
import pandas as pd
df = pd.DataFrame({'link':['=HYPERLINK("http://www.someurl.com", "some website")']})
df.to_excel('mohammad-rocks.xlsx')https://stackoverflow.com/questions/48489893
复制相似问题