[英]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.