Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 295
RustyShackleford Avatar asked Dec 03 '25 08:12

RustyShackleford


2 Answers

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
like image 160
cs95 Avatar answered Dec 04 '25 22:12

cs95


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

like image 23
EdChum Avatar answered Dec 04 '25 20:12

EdChum



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!