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



    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) &lt;ipython-input-33-d9b73bdc4f8e&gt; in &lt;module&gt;()
----&gt; 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
-&gt; 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()

&lt;ipython-input-33-d9b73bdc4f8e&gt; in &lt;lambda&gt;(x)
----&gt; 1 data["email_id"] = data.COMMENTS.apply(lambda x: email_extract(x))

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

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

TypeError: expected string or bytes-like object



How can I fix this issue?
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))