Extract email from a column of the DataFrame

In this python pandas program, we will extract email from a column of the DataFrame using the pandas library.

Steps to solve the program
  1. Import pandas library as pd.
  2. Import re library.
  3. Create a dataframe using pd.DataFrame().
  4. Create a function to extract email from the record.
  5. Extract email from the record using re.findall(r'[\w\.-]+@[\w\.-]+’,str(text)).
  6. It will extract email from the record and return will give email as the output of the function.
  7. Now apply this function to the company name column of the dataframe using df[‘Company_mail’].apply(lambda x: find_email(x)).
  8. The lambda function will apply the created function to each row to extract email and store it in the new column.
  9. 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
				
			

get the length of the integer of a column in a DataFrame

extract the hash attached word from Twitter text from the specified column of a given DataFrame

Leave a Comment