I have a dataframe that looks like this:
for user in df_enlaces['CorreoElectronico'].values:
to_mail=(df_enlaces.set_index('CorreoElectronico').loc[user].index[0])
print(to_mail, df_enlaces.set_index('CorreoElectronico').loc[user]['Enlace'])
The output of the above code is:
example1@mail.com CorreoElectronico
example1@mail.com link1
example1@mail.com link2
Name: Enlace, dtype: object
example1@mail.com CorreoElectronico
example1@mail.com link1
example1@mail.com link2
Name: Enlace, dtype: object
example2@mail.com CorreoElectronico
example2@mail.com link3
example2@mail.com link4
example2@mail.com link5
Name: Enlace, dtype: object
example2@mail.com CorreoElectronico
example2@mail.com link3
example2@mail.com link4
example2@mail.com link5
Name: Enlace, dtype: object
example2@mail.com CorreoElectronico
example2@mail.com link3
example2@mail.com link4
example2@mail.com link5
Name: Enlace, dtype: object
However, it is repeated. Something I am doing wrong when looping. I just want to match the user email with its link in the column ['Enlace']. The desired output would be:
example1@mail.com link1
example1@mail.com link2
example2@mail.com link3
example2@mail.com link4
example2@mail.com link5
try this
for user in df_enlaces['CorreoElectronico'].unique():
....
You are looping through each value in the CorreoElectronico
column and then when you perform .loc
it pulls out every row which matches that user (and there are several)
It seems like this should do the trick:
users = ['user1', 'user1', 'user2', 'user2', 'user3']
links = ['link1', 'link2', 'link3', 'link4', 'link5']
df = pd.DataFrame({'user':users,'link':links})
for index, row in df.iterrows():
print(row['user'], row['link'])
At least it does when you just want to print the user and link from each row in the dataframe? This outputs:
user1 link1
user1 link2
user2 link3
user2 link4
user3 link5
As I suppose, you want to:
To do it, use the following loop:
for _, row in df_enlaces.iterrows():
print(f'{row.CorreoElectronico} {row.Enlace}')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.