简体   繁体   English

如何正确循环熊猫数据框?

[英]How can I loop correctly a pandas dataframe?

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'].我只想将用户电子邮件与其在列 ['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)您正在遍历CorreoElectronico列中的每个值,然后当您执行.loc它会拉出与该用户匹配的每一行(并且有几个)

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:正如我想的那样,您想要:

  • iterate over rows of df_enlaces ,迭代df_enlaces行,
  • from each row print the e-mail address ( CorreoElectronico ) and the link ( Enlace ).从每一行打印电子邮件地址 ( CorreoElectronico ) 和链接 ( Enlace )。

To do it, use the following loop:为此,请使用以下循环:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM