RustyShackleford
RustyShackleford

Reputation: 3677

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. [email protected] becomes [email protected]

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.

Upvotes: 3

Views: 1033

Answers (2)

cs95
cs95

Reputation: 402814

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  [email protected]
1    [email protected]
2    [email protected]

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

0    [email protected]
1      [email protected]
2       [email protected]
Name: ADDR, dtype: object

Upvotes: 4

EdChum
EdChum

Reputation: 394159

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

In[72]:
df = pd.DataFrame({'email':['[email protected]','[email protected]','[email protected]']})
df

Out[72]: 
              email
0    [email protected]
1      [email protected]
2  [email protected]

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

Out[73]: 
           email
0  [email protected]
1  [email protected]
2  [email protected]

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

Upvotes: 2

Related Questions