[英]A regex pattern that matches all words starting from a word with an s and stopping before a word that starts with an s
[英]Get all the emails and the word just before the email starts
我正在嘗試解析我的數據集以獲取 email 之前的所有電子郵件和單詞。 例如,如果我有這樣的一行:
sno text
1 From: m.kro@b.org To: Cha.Sh@dys.com Hi my name is Sam and my email is samwise@gmail.com
然后我想將其捕獲為:
sno text emails
1 From: m.kro@b.org To: Cha.Sh@dys.com Hi my name is Sam and my email is samwise@gmail.com [From : m.kro@b.org ,To: Cha.Sh@dys.com, is samwise@gmail.com]
到目前為止我嘗試過的解決方案:
我已經嘗試使用“find_all”function 來獲取所有電子郵件,但是在 email 開始之前我遇到了問題。
df['Full Comments'].str.findall('(\S+@\S+)').str[0]
對此的任何幫助表示贊賞。 謝謝你。
嘗試:
pat = '([\w:]+ [\w\.]+@[\w\.]+)'
df['emails'] = df.text.str.extractall(pat).groupby(level=0)[0].agg(list)
更新:您可以使用unstack
將單詞提升為列標題:
emails = (df.text.str.extractall(pat)
.reset_index('match', drop=True)
.set_index([0],append=True)[1]
.unstack()
)
df = df.join(電子郵件)
Output(不含連接部分):
0 From: To: is
0 m.kro@b.org Cha.Sh@dys.com samwise@gmail.com
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.