Home > Net >  How to filter rows by custom function Pandas?
How to filter rows by custom function Pandas?

Time:05-24

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)

CodePudding user response:

you can find the answer here George

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)]

Hope you find this helpful!

  • Related