简体   繁体   English

Python:遍历Pandas DataFrame以匹配列表中的字符串

[英]Python: Looping through Pandas DataFrame to match string in a list

My question is regarding a Pandas DataFrame and a list of e-mail addresses. 我的问题是关于Pandas DataFrame和电子邮件地址列表的。 The simplified dataframe (called 'df') looks like this: 简化的数据帧(称为“ df”)如下所示:

   Name    Address         Email
0  Bush    Apple Street
1  Volt    Orange Street
2  Smith   Kiwi Street

The simplified list of e-mail addresses looks like this: 电子邮件地址的简化列表如下所示:

list_of_emails = ['johnsmith@gmail.com', 'judyvolt@hotmail.com', 'bush@yahoo.com']

Is it possible to loop through the dataframe, to check if a last name is (part of) a e-mail address AND then add that email address to the dataframe? 是否可以遍历数据框,检查姓氏是否是电子邮件地址(的一部分),然后将该电子邮件地址添加到数据框? The following code does not work unfortunately, because of line 2 I think: 由于我认为第2行,以下代码无法正常运行:

for index, row in df.iterrows():
    if row['Name'] in x for x in list_of_emails:
        df['Email'][index] = x

Your help is very much appreciated! 非常感激你的帮助!

Generally you should consider using iterrows as last resort only. 通常,您应该考虑仅将iterrows作为最后的手段。

Consider this: 考虑一下:

import pandas as pd

df = pd.DataFrame({'Name': ['Smith', 'Volt', 'Bush']})

list_of_emails = ['johnsmith@gmail.com', 'judyvolt@hotmail.com', 'bush@yahoo.com']

def foo(name):
    for email in list_of_emails:
        if name.lower() in email:
            return email

df['Email'] = df['Name'].apply(foo)

print(df)

#     Name                 Email
# 0  Smith   johnsmith@gmail.com
# 1   Volt  judyvolt@hotmail.com
# 2   Bush        bush@yahoo.com

Here's one way using apply and lambda function 这是使用apply和Lambda函数的一种方法

For, first match 首先,

In [450]: df.Name.apply(
           lambda x: next((e for e in list_of_emails if x.lower() in e), None))
Out[450]:
0     johnsmith@gmail.com
1    judyvolt@hotmail.com
2          bush@yahoo.com
Name: Name, dtype: object

For all matches, in a list 对于所有比赛,在列表中

In [451]: df.Name.apply(lambda x: [e for e in list_of_emails if x.lower() in e])
Out[451]:
0     [johnsmith@gmail.com]
1    [judyvolt@hotmail.com]
2          [bush@yahoo.com]
Name: Name, dtype: object

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

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