Delete all records with invalid email. Pandas

Viewed 189

How to delete all records in Pandas Dataframe with invalid email (doesn't have name , domain, at sign)

My code looks like :

import re
regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'  

def validate_email(email):
    return re.search(regex, email)

all_data = all_data.loc[all_data['Email'].apply(validate_email)]

but I have error:

KeyError: "None of [Index([                                                       None,\n                                                              None,\n                                                              None,\n                                                              None,\n                                                              None,\n         <re.Match object; span=(0, 16), match='adam@example.com'>,\n                                         

how do it correctly ??

2 Answers

First, you need to use r before the regex to make escape characters work.

regex = r'^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'  

Then, you can simply use the str.contains method.

all_data = all_data[all_data['Email'].str.contains(regex)]

Your code works if you change your validation function a little bit

regex = '^[a-z0-9]+[\._]?[a-z0-9]+[@]\w+[.]\w{2,3}$'  

def validate_email(email):
    if re.search(regex, email):
        return True
    return False

all_data = all_data.loc[all_data['Email'].apply(validate_email)]
Related