I am extracting email ids and storing them into a new column variable, but I am getting the issue:

enter image description here

    import re
    def email_extract(comments):
        comments1 =re.findall(r'[\w\.-]+@[\w\.-]+',comments)
       return comments1

   data["email_id"] = data.COMMENTS.apply(lambda x: email_extract(x))

--- TypeError                                 Traceback (most recent call
last) <ipython-input-33-d9b73bdc4f8e> in <module>()
----> 1 data["email_id"] = data.COMMENTS.apply(lambda x: email_extract(x))

C:\ProgramData\Anaconda4\lib\site-packages\pandas\core\series.py in
apply(self, func, convert_dtype, args, **kwds)    3192            
else:    3193                 values = self.astype(object).values
-> 3194                 mapped = lib.map_infer(values, f, convert=convert_dtype)    
3195     3196         if len(mapped) and
isinstance(mapped[0], Series):

pandas/_libs/src\inference.pyx in pandas._libs.lib.map_infer()

<ipython-input-33-d9b73bdc4f8e> in <lambda>(x)
----> 1 data["email_id"] = data.COMMENTS.apply(lambda x: email_extract(x))

<ipython-input-32-97f3705d1972> in email_extract(comments)
      2 def email_extract(comments):
      3     #re_pattern = re.compile(r'[\w\.-]+@[\w\.-]+')
----> 4     comments1 =re.findall(r'[\w\.-]+@[\w\.-]+',comments)
      5     return comments1

C:\ProgramData\Anaconda4\lib\re.py in findall(pattern, string, flags)
    222     Empty matches are included in the result."""
--> 223     return _compile(pattern, flags).findall(string)
    225 def finditer(pattern, string, flags=0):

TypeError: expected string or bytes-like object

How can I fix this issue?

  • $\begingroup$ seems to be no string... try str(comment) $\endgroup$
    – Peter
    Commented May 27, 2019 at 21:29

1 Answer 1


You may have missing data (e.g. np.nans) in your COMMENTS field. This will throw an error in your email_extract function.
Try to filter the problematic rows out, e.g. with

filtered_data = data[~data.COMMENTS.isna()] #purge problematic comments

and applying the email extraction to the now "clean" column:

filtered_data.COMMENTS.apply(lambda x: email_extract(x))
  • $\begingroup$ yes comments had null value.It worked after filtering null. Thanks $\endgroup$
    – Taylor
    Commented May 29, 2019 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.