I would like to do a simple email validation for list import of email addresses into a database. I just want to make sure that there is content before the @ sign, an @ sign, content after the @ sign, and 2+ characters after the '.' . Here is a sample df:
import pandas as pd
import re
errors= {}
data= {'First Name': ['Sally', 'Bob', 'Sue', 'Tom', 'Will'],
'Last Name': ['William', '', 'Wright', 'Smith','Thomas'],
'Email Address': ['[email protected]','[email protected]','[email protected]','[email protected]','']}
df=pd.DataFrame(data)
This is the expression I was using to check for valid emails:
regex = re.compile(r'([A-Za-z0-9]+[.-_])*[A-Za-z0-9]+@[A-Za-z0-9-]+(\.[A-Z|a-z]{2,})+')
def isValid(email):
if re.fullmatch(regex, email):
pass
else:
return("Invalid email")
This regex is working fine but I am not sure how to easily loop through my entire df email address column. I have tried:
for col in df['Email Address'].columns:
for i in df['Email Address'].index:
if df.loc[i,col] = 'Invalid email'
errors={'row':i, 'column':col, 'message': 'this is not a valid email address'
I am wanting to write the invalid email to a dictionary titled errors. with the above code I get an invalid error.