简体   繁体   中英

How to replace part of email address with another string in pandas?

I have a dataframe with email addresses. I need to replace every ending of email address with a '.mn'. What I mean by ending is '.org', '.com', etc.

Ex. John@smith.com becomes John@smith.mn

Not sure what I am doing wrong.

This is what I have so far, but this is not replacing or giving me an error message:

email['ADDR'] = email['ADDR'].str.replace(r'[.]{2,}', '.mn')

Thank you in advance.

This should do:

email['ADDR'] = email['ADDR'].str.replace('.{3}$', 'mn')

If you need to handle variable length domains ( .edu , .com1 , and so on), you can use:

email

             ADDR
0  john@smith.com
1    test@abc.edu
2    foo@bar.abcd

email['ADDR'].str.replace('\..{2,}$', '.mn')

0    john@smith.mn
1      test@abc.mn
2       foo@bar.mn
Name: ADDR, dtype: object

Another method which will handle variable length top-level endings is to use str.rsplit :

In[72]:
df = pd.DataFrame({'email':['John@smith.com','John@smith.x','John@smith.hello']})
df

Out[72]: 
              email
0    John@smith.com
1      John@smith.x
2  John@smith.hello

In[73]:
df['email'] = df['email'].str.rsplit('.').str[0] +'.mn'
df

Out[73]: 
           email
0  John@smith.mn
1  John@smith.mn
2  John@smith.mn

This will find the last trailing dot, takes the left hand side and append the new desired suffix

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