Python Practice › Pandas Programs › Program 76
Extract emails
Write a pandas program to extract email addresses from a column.
Output
Company_mail email 0 TCS tcs@yahoo.com tcs@yahoo.com 1 Apple apple@icloud.com apple@icloud.com 2 Google google@gmail.com google@gmail.com
Approach
str.extractwith a regex group.
Solution
import pandas as pddf = pd.DataFrame({"Company_mail": ["TCS tcs@yahoo.com", "Apple apple@icloud.com", "Google google@gmail.com"]})df["email"] = df["Company_mail"].str.extract(r"([\w.+-]+@[\w-]+\.[\w.]+)", expand=False)print(df)Output
Company_mail email 0 TCS tcs@yahoo.com tcs@yahoo.com 1 Apple apple@icloud.com apple@icloud.com 2 Google google@gmail.com google@gmail.com
Learn the concept
New to this topic? Read the lesson, then come back.