How to Extract an Email Address From Text in a Dataframe

Python

 1|  import re
 2|  
 3|  #Create dataframe from dictionary
 4|  data = {'id':[112,312,34,654],
 5|            'text': ['Email Address: akilest@gmail.com',
 6|                   'Email Address: mjikn789@gmail.com',
 7|                   'Email Address: fourtest@hotmail.com',
 8|                   'Email Address: notfor789@hotmail.com']}
 9|  df = pd.DataFrame(data)
10|  
11|  #Use regex to extract the email address from the text column
12|  df['email'] = df['text'].apply(lambda x: re.findall(r"([\w.-]+@[\w.-]+)",x)[0])
13|  
14|  >>
15|  	id	text					email
16|  0	112	Email Address: akilest@gmail.com	akilest@gmail.com
17|  1	312	Email Address: mjikn789@gmail.com	mjikn789@gmail.com
18|  2	34	Email Address: fourtest@hotmail.com	fourtest@hotmail.com
19|  3	654	Email Address: notfor789@hotmail.com	notfor789@hotmail.com
Nlp | Re | Email
Did you find this snippet useful?

Sign up for free to to add this to your code library