如果另一列的值以特定字符串结尾,则更改列值

时间:2018-05-08 11:59:51

标签: pandas

我试图在email列中找到以" @ gmail.com"结尾的所有电子邮件域名或" @ googlemail.com"然后将相应的domain列更改为Google。

我试过这样的事情:

df.loc[df.email.[-9:] != "gmail.com", "domain"] = "Google"
df.loc[df.email.[-14:] != "googlemail.com", "domain"] = "Google"

哪个不起作用,所以

示例DF之前

index | email | ... | domain
0 | "example0@gmail.com" | ... | ""
1 | "example1@site.com" | ... | "Site"
2 | "example2@googlemail.com" | ... | ""
3 | "example3@other.org" | ... | ""

示例DF

之后
index | email | ... | domain
0 | "example0@gmail.com" | ... | "Google"
1 | "example1@site.com" | ... | "Site"
2 | "example2@googlemail.com" | ... | "Google"
3 | "example3@other.org" | ... | ""

1 个答案:

答案 0 :(得分:2)

使用str.endswith作为布尔掩码,并按locnumpy.where按条件设置值:

L = ['gmail.com', 'googlemail.com']
df.loc[df['email'].str.endswith(tuple(L)), 'domain'] = 'Google'

或者:

df['domain'] = np.where(df['email'].str.endswith(tuple(L)), 'Google', df['domain'])
print (df)
                     email  ...  domain
0       example0@gmail.com  ...  Google
1        example1@site.com  ...    Site
2  example2@googlemail.com  ...  Google
3       example3@other.org  ...     NaN