如何避免南进np.哪里在两个数据帧之间?

2024-05-16 21:43:58 发布

您现在位置:Python中文网/ 问答频道 /正文

我有客户数据,想检查他们的电子邮件是否存在于单独的数据框中np.哪里,返回“match”或“no match”。你知道吗

但是,其中一封客户电子邮件是NaN,第二个df中的一封电子邮件是NaN,所以这是一个匹配项。如您所见,已找到stack overflow先生的匹配项。你知道吗

customers = pd.DataFrame({'firstname':['stack','Bar Bar','Foo Bar','jim','john','mary','jim'],
                   'lastname':['overflow','Bar','Foo Bar','ryan','con','sullivan','Ryan'],
                   'email':[np.nan,'Bar','Foo Bar','jim@com','john@com','mary@com','Jim@com']})

customers

    firstname   lastname    email
0   jim             bob             NaN
1   Bar Bar     Bar     bar@com
2   Foo Bar     Foo Bar     foo@com
3   jim     ryan        jim@com
4   john        con     john@com
5   mary        sullivan    hello@com
6   jim     Ryan        jon@com

现在我要检查他们的电子邮件是否在另一个名为“电子邮件”的数据框中:


emails = pd.DataFrame({'emails':['mary@com','bar@com','foo@com','jim@com','john@com',np.nan,'jon@com']})

emails

    emails
0   mary@com
1   bar@com
2   foo@com
3   jim@com
4   john@com
5   NaN
6   jon@com

我将创建一个名为“check”的新列,它将检查结果记录为“match”或“no match”


customers['check'] = np.where(customers['email'].isin(emails['emails']), 'match', 'no_match')

customers


    firstname   lastname    email       check
0   jim     bob     NaN     match
1   Bar Bar     Bar     bar@com     match
2   Foo Bar     Foo Bar     foo@com     match
3   jim     ryan        jim@com     match
4   john        con     john@com    match
5   mary        sullivan    hello@com   no_match
6   jim     Ryan        jon@com     match

除了吉姆·鲍勃的唱片外,一切看起来都很好。他的邮件是NaN,邮件数据框里有个NaN。所以它作为匹配返回。你知道吗

最好的办法是什么?你知道吗

我在考虑做一些激烈的事情,比如fillna(),然后把它改成一个字符串,比如'fakeNaN'或者别的什么,这样就不会产生匹配了。但一定有更好的办法。你知道吗

编辑:我刚刚试过这个:

定义了一个与lambda一起使用的函数,如果客户没有电子邮件,则不返回电子邮件。你知道吗

def lam(r):

# if the email is nan, return no_email

    if r == np.nan:
        return 'no_email'

    elif r in emails['emails']:
        return 'match'

    elif not r in emails['emails']:
        return 'no_match'

# apply this lambda operation to the customer email row and return results to customer['check']

customers['check'] = customers.apply(lambda row: lam(row['email']), axis=1)

但是现在它不会返回任何匹配项。有几根火柴。你知道吗

0    no_match
1    no_match
2    no_match
3    no_match
4    no_match
5    no_match
6    no_match
dtype: object

我现在注意到一些奇怪的事情。你知道吗

我可以查看emails['emails']并看到jim@com是否存在:

emails['emails']

0    mary@com
1     bar@com
2     foo@com
3     jim@com
4    john@com
5         NaN
6     jon@com
Name: emails, dtype: object

为什么这不管用呢?你知道吗

'jim@com' in emails['emails']

False

Tags: nocomfoo电子邮件emailmatchnpbar
2条回答

isinnp.select

m1=customers.email.isin(emails.emails.dropna().values)
m2=customers.email.notna()
customers['check']=np.select([m1&m2,~m1&m2],['match','no match'],default='no_email')
customers
  firstname  lastname     email     check
0     stack  overflow       NaN  no_email        
1   Bar Bar       Bar       Bar  no match
2   Foo Bar   Foo Bar   Foo Bar  no match
3       jim      ryan   jim@com     match
4      john       con  john@com     match
5      mary  sullivan  mary@com     match
6       jim      Ryan   Jim@com  no match

将电子邮件保存为熊猫系列。有点不正统的做法。你知道吗

*1用于将布尔值转换为整数。你知道吗

emails = pd.Series(['mary@com','bar@com','foo@com','jim@com','john@com',np.nan,'jon@com'])

(customers['email'].isin(emails)*1+customers['email'].isnull()*1).map({0:'No-Match',1:'Match',2:'No-Record'})

0   No-Record
1   No-Match
2   No-Match
3   Match
4   Match
5   Match
6   No-Match

相关问题 更多 >