[英]How to replace part of email address with another string in pandas?
我有一個帶有電子郵件地址的數據框。 我需要用'.mn'替換每個電子郵件地址的結尾。 結尾的意思是'.org','。com'等。
Ex. John@smith.com becomes John@smith.mn
不確定我做錯了什么。
這是我到目前為止,但這不是替換或給我一個錯誤信息:
email['ADDR'] = email['ADDR'].str.replace(r'[.]{2,}', '.mn')
先感謝您。
這應該做:
email['ADDR'] = email['ADDR'].str.replace('.{3}$', 'mn')
如果需要處理可變長度域( .edu
, .com1
等),可以使用:
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
另一種處理可變長度頂級結尾的方法是使用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
這將找到最后一個尾隨點,左側並附加新的所需后綴
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.