[英]For loop not iterating as desired in Python
我有一个包含要发送的电子邮件和链接的数据框。 我用来遍历列表并将其对应链接发送给每个用户的代码片段是:
boton_list=[]
for arch, user in enumerate(df_enlaces['CorreoElectronico'].values):
enlace=df_enlaces.set_index('CorreoElectronico').loc[user]['Enlace']
conteo=0
print(arch,user,enlace)
while conteo<(len(enlace)):
boton=f"""<a href={enlace[arch]} > <button style='background-color: orange'; border-color:blue> LINK </button> </a>"""
boton_list.append(boton)
conteo+=1
但是,“print(enlace)”会打印以下内容:
我的数据框看起来像:
只有两行和一个用户(在本例中)并返回 4 行?
此外, boton_list 返回:
["<a href=link1 > <button style='background-color: orange'; border-color:blue> LINK </button> </a>",
"<a href=link1 > <button style='background-color: orange'; border-color:blue> LINK </button> </a>",
"<a href=link2 > <button style='background-color: orange'; border-color:blue> LINK </button> </a>",
"<a href=link2 > <button style='background-color: orange'; border-color:blue> LINK </button> </a>"]
不返回对应于第一行的 link1。 我错过了一些东西,我无法找到方法。
如果我正确理解问题,则无需循环,只需直接应用您的格式:
button_template = """<a href={}><button style='background-color: orange'; border-color:blue> LINK </button> </a>"""
df_enlaces['Enlace'] = df_enalces['Enlace'].transform(lambda c: button_template.format(c))
现在您可以将其导出到列表:
df_enlaces['Enlace'].to_list()
或者,如果您想要多个用户/电子邮件的嵌套列表:
df_enlaces.groupby('CorreoElectronico').agg(buttons=('Enlace', list))['buttons'].to_list()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.