我有一个Pandas 数据框,里面有以下数据

data = {'product_id': [11,11,11,11],
        'rec_prod_id':[12,13,14,15],
        'product_URL': ['www.example.com','www.example4.com','www.example3.com','www.example2.com']}
data = pd.DataFrame(data)

我的目标是

  1. 对product_id进行分组并执行Aggregation-nUnique AND LIST

  2. 将数据框导出到EXCEL文件时,将每个rec_prod_id的product_id转换为可点击(这将把我带到相应的网页)

因此,我try 了下面的方法,参考了SO帖子here

def create_hyperlink(row):
    return '<a href="{0}">{1}</a>'.format(row['product_id'], row['product_URL'])
data['product_hyperlinks'] = data.apply(create_hyperlink, axis=1)
data.groupby('product_id').agg{('# of recs'=product_id:nunique,product_links:list)}

但当我导出EXCEL文件时,上面的结果会导致意外和不正确的输出(我在输出中得到了如下所示的原始URL

enter image description here

But I expect my output to be like below

enter image description here

推荐答案

在您的函数中使用HYPERLINK函数,然后设置可点击的URL:

data = {'product_id': [11, 22, 31, 45],
        'product_URL': ['https://www.google.com','https://www.google.com',
                        'https://www.google.com','https://www.google.com']}
data = pd.DataFrame(data)

def create_hyperlink(row):
    return '=HYPERLINK("{1}", "{0}")'.format(row['product_id'], row['product_URL'])

data['product_hyperlinks'] = data.apply(create_hyperlink, axis=1)

#https://stackoverflow.com/a/73489111/2901002
writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
data.to_excel(writer, index=False)
    
# Get the xlsxwriter objects from the dataframe writer object.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

# Get the default URL format.
url_format = workbook.get_default_url_format()

# Close the Pandas Excel writer and output the Excel file.
writer.close()

pic

对于HMTL,使用您的解决方案(交换{0}和{1}),并将escape=False添加到DataFrame.to_html:

def create_hyperlink(row):
    return '<a href="{1}">{0}</a>'.format(row['product_id'], row['product_URL'])


data['product_hyperlinks'] = data.apply(create_hyperlink, axis=1)


data.to_html('file.html', index=False, escape=False)

编辑:循环中写入指向Excel的链接的替代解决方案:

data = {'product_id': [11, 22, 31, 45],
        'product_URL': ['https://www.google.com','https://www.google.com',
                        'https://www.google.com','https://www.google.com']}
data = pd.DataFrame(data)


writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')

# Convert the dataframe to an XlsxWriter Excel object.
data.to_excel(writer, index=False)


# Get the xlsxwriter objects from the dataframe writer object.
workbook  = writer.book
worksheet = writer.sheets['Sheet1']

data['product_hyperlinks'] = ""

pos = data.columns.get_loc('product_hyperlinks')

for i, (name, link) in enumerate(zip(data['product_id'], data['product_URL'])):
    worksheet.write_url(i+1, pos, link, None, str(name))

# Close the Pandas Excel writer and output the Excel file.
writer.close()

Python相关问答推荐

根据在同一数据框中的查找向数据框添加值

删除最后一个pip安装的包

不理解Value错误:在Python中使用迭代对象设置时必须具有相等的len键和值

将两只Pandas rame乘以指数

无法通过python-jira访问jira工作日志(log)中的 comments

运行终端命令时出现问题:pip start anonymous"

组/群集按字符串中的子字符串或子字符串中的字符串轮询数据框

如何启动下载并在不击中磁盘的情况下呈现响应?

* 动态地 * 修饰Python中的递归函数

AES—256—CBC加密在Python和PHP中返回不同的结果,HELPPP

pandas:对多级列框架的列进行排序/重新排序

重置PD帧中的值

python sklearn ValueError:使用序列设置数组元素

从旋转的DF查询非NaN值

仅使用预先计算的排序获取排序元素

如果服务器设置为不侦听创建,则QWebSocket客户端不连接到QWebSocketServer;如果服务器稍后开始侦听,则不连接

一维不匹配两个数组上的广义ufunc

Match-Case构造中的对象可调用性测试

Pandas:根据相邻行之间的差异过滤数据帧

401使用有效的OAuth令牌向Google Apps脚本Web App发出POST请求时出现未经授权的错误(";