How to filter rows by custom function Pandas?

Question:

I have a custom regex Python function that checks is it email or not:

def isEmail(str):
  return True;

I want to iterate all rows in Pandas dataframe and validate the column email. and return count ofvalid rows (true/false).

I have found apply() Pandas function.

I try to leave only rows where column email has correct email address:

def isEmail(str):
    return re.search('regex', str)

    dt[isEmail(dt['email'])])

Then call this again to count how much incorrect rows to put into Python set:

incorrectEmails = {emails: 0}
count = dt[isEmail(dt['email'])])
incorrectEmails.set(count)
Asked By: George

||

Answers:

You can find the answer here:

Is it possible to use a custom filter function in pandas?

You can try adding a global counter inside the is_email() function to count how many falses were provided and use .apply() on the email column.

dt2 = dt[dt['email'].apply(is_email)]
Answered By: Ahmad Othman
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.