Home > Back-end >  How can I loop correctly a pandas dataframe?
How can I loop correctly a pandas dataframe?

Time:10-27

I have a dataframe that looks like this:

enter image description here

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:

[email protected] CorreoElectronico
[email protected]    link1
[email protected]    link2
Name: Enlace, dtype: object
[email protected] CorreoElectronico
[email protected]    link1
[email protected]    link2
Name: Enlace, dtype: object
[email protected] CorreoElectronico
[email protected]    link3
[email protected]    link4
[email protected]    link5
Name: Enlace, dtype: object
[email protected] CorreoElectronico
[email protected]    link3
[email protected]    link4
[email protected]    link5
Name: Enlace, dtype: object
[email protected] CorreoElectronico
[email protected]    link3
[email protected]    link4
[email protected]    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:

[email protected]    link1
[email protected]    link2
[email protected]    link3
[email protected]    link4
[email protected]    link5

CodePudding user response:

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)

CodePudding user response:

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

CodePudding user response:

As I suppose, you want to:

  • iterate over rows of df_enlaces,
  • from each row print the e-mail address (CorreoElectronico) and the link (Enlace).

To do it, use the following loop:

for _, row in df_enlaces.iterrows():
    print(f'{row.CorreoElectronico}    {row.Enlace}')
  • Related