1

I'm using python 3.2.2 on windows 7.This is part of my code.it reads from an excel file .But when I run the code it just prints from 0 to 10 and gives" TypeError: 'float' object is not iterable". Thanks for any help!

 pages = [i for i in range(0,19634)]


    for page in  pages:

 x=df.loc[page,["id"]]
 x=x.values
 x=str(x)[2:-2]
 text=df.loc[page,["rev"]]

 def remove_punct(text):
  text=''.join([ch.lower() for ch in text if ch not in exclude])
  tokens = re.split('\W+', text)
  tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

  removetable = str.maketrans('', '', '1234567890')
  out_list = [s.translate(removetable) for s in tokens1] 
  str_list = list(filter(None,out_list)) 
  line = [i for i in str_list if len(i) > 1]

  return line

 s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))

 with open('FileNamex.csv', 'a', encoding="utf-8") as f:
     s.to_csv(f, header=False)

 print(s)

this is the Error

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-c71f66bdaca6> in <module>()
     33   return line
     34 
---> 35  s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))
     36 
     37  with open('FileNamex.csv', 'a', encoding="utf-8") as f:

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

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

<ipython-input-54-c71f66bdaca6> in <lambda>(x)
     33   return line
     34 
---> 35  s=df.loc[page,["rev"]].apply(lambda x:remove_punct(x))
     36 
     37  with open('FileNamex.csv', 'a', encoding="utf-8") as f:

<ipython-input-54-c71f66bdaca6> in remove_punct(text)
     22 
     23  def remove_punct(text):
---> 24   text=''.join([ch.lower() for ch in text if ch not in exclude])
     25   tokens = re.split('\W+', text)
     26   tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

TypeError: 'float' object is not iterable

Thanks for any help!

1 Answer 1

1

You are trying to apply a function that iterates text (whatever it is) - and ou call it using a float value.

floats can not be iterated. You can use text = str(text) to convert any input to text first - but looking at your code I hesitate to say that would make sense.

You can check if you are handling a float like this:

def remove_punct(text):

     if isinstance(text,float): 
         pass   #    do something sensible with floats here
         return #    something sensible

     text=''.join([ch.lower() for ch in text if ch not in exclude])
     tokens = re.split('\W+', text)
     tex = " ".join([wn.lemmatize(word) for word in tokens if word not in stopword])

     removetable = str.maketrans('', '', '1234567890')
     out_list = [s.translate(removetable) for s in tokens1] 
     str_list = list(filter(None,out_list)) 
     line = [i for i in str_list if len(i) > 1]

     return line

You can either tackle float via isinstance or get inspiration from In Python, how do I determine if an object is iterable? on how to detect if you provide any iterable. You need to handle non-iterables differently.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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