简体   繁体   中英

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. The simplified dataframe (called 'df') looks like this:

   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:

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.

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

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

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.

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