将超链接添加到由 pandas 数据框 to_excel 方法创建的 Excel 表 [英] add hyperlink to excel sheet created by pandas dataframe to_excel method

查看:37
本文介绍了将超链接添加到由 pandas 数据框 to_excel 方法创建的 Excel 表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用 pandas DataFrame 转换为 Excel 工作表.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.to_excel.html" rel="noreferrer">df.to_excel.

I have converted a pandas DataFrame to an Excel sheet using df.to_excel.

现在,我想为一列中的值添加超链接.换句话说,当客户看到我的 Excel 表格时,他将能够点击一个单元格并打开一个网页(取决于该单元格中的值).

Now, I want to add hyperlinks to the values in one column. In other words, when a customer sees my excel sheet, he would be able to click on a cell and bring up a webpage (depending on the value in this cell).

推荐答案

基于@guillaume-jacquenot 的方法,我们可以使用 apply 将其应用于整个系列.

Building on the approach by @guillaume-jacquenot we can use apply to apply this to an entire Series.

df = pd.DataFrame({'Year': [2000, 2001, 2002 , 2003]})

为了干净,我写了一个辅助方法.

For cleanliness, I wrote a helper method.

def make_hyperlink(value):
    url = "https://custom.url/{}"
    return '=HYPERLINK("%s", "%s")' % (url.format(value), value)

然后,应用到系列:

df['hyperlink'] = df['Year'].apply(lambda x: make_hyperlink(x))

>

    Year    hyperlink
0   2000    =HYPERLINK("https://custom.url/2000", "2000")
1   2001    =HYPERLINK("https://custom.url/2001", "2001")
2   2002    =HYPERLINK("https://custom.url/2002", "2002")
3   2003    =HYPERLINK("https://custom.url/2003", "2003")

这篇关于将超链接添加到由 pandas 数据框 to_excel 方法创建的 Excel 表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆