In this python pandas program, we will extract email from a column of the DataFrame using the pandas library.
Steps to solve the program
- Import pandas library as pd.
- Import re library.
- Create a dataframe using pd.DataFrame().
- Create a function to extract email from the record.
- Extract email from the record using re.findall(r'[\w\.-]+@[\w\.-]+’,str(text)).
- It will extract email from the record and return will give email as the output of the function.
- Now apply this function to the company name column of the dataframe using df[‘Company_mail’].apply(lambda x: find_email(x)).
- The lambda function will apply the created function to each row to extract email and store it in the new column.
- Print the output.
import pandas as pd
import re
d = {'Company_mail':['TCS tcs@yahoo.com','Apple apple@icloud.com','Google google@gmail.com']}
df = pd.DataFrame(d)
print(df)
def find_email(text):
email = re.findall(r'[\w\.-]+@[\w\.-]+',str(text))
return ",".join(email)
df['email']=df['Company_mail'].apply(lambda x: find_email(x))
print("Extracting email from dataframe columns:")
print(df)
Output :
0 Company_mail
0 TCS tcs@yahoo.com
1 Apple apple@icloud.com
2 Google google@gmail.com
Extracting email from dataframe columns:
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