TypeError: "float" object is not iterable

64 views Asked by At

The first thing is that I import a .csv file and I´m working some tasks without any problem, but now I have this issue that I´m not able to solve. After defining a function I expected to remove all punctuations from the dataset. But I received a series of errors that I didn´t expect, I was following the same steps of an online course but this happen and I follow behind.

# import the data using read_csv
data = pd.read_csv("text_data.csv")
data

# Let's define a function to remove punctuations

def punc(message):
    no_punc = [char for char in message if char not in string.punctuation]
    join_punc = "".join(no_punc)

    return join_punc

# Let's remove punctuations from our dataset 
data["new_verified_reviews"] = data["verified_reviews"].apply(punc)


**TypeError                                 Traceback (most recent call last)**
Cell In[62], line 2
      1 # Let's remove punctuations from our dataset 
----> 2 data["new_verified_reviews"] = data["verified_reviews"].apply(punc)

File ~\AppData\Local\anaconda3\envs\myenv\lib\site-packages\pandas\core\series.py:4753, in Series.apply(self, func, convert_dtype, args, by_row, **kwargs)
   4625 def apply(
   4626     self,
   4627     func: AggFuncType,
   (...)
   4632     **kwargs,
   4633 ) -> DataFrame | Series:
   4634     """
   4635     Invoke function on values of Series.
   4636 
   (...)
   4751     dtype: float64
   4752     """
-> 4753     return SeriesApply(
   4754         self,
   4755         func,
   4756         convert_dtype=convert_dtype,
   4757         by_row=by_row,
   4758         args=args,
   4759         kwargs=kwargs,
   4760     ).apply()

File ~\AppData\Local\anaconda3\envs\myenv\lib\site-packages\pandas\core\apply.py:1207, in SeriesApply.apply(self)
   1204     return self.apply_compat()
   1206 # self.func is Callable
-> 1207 return self.apply_standard()

File ~\AppData\Local\anaconda3\envs\myenv\lib\site-packages\pandas\core\apply.py:1287, in SeriesApply.apply_standard(self)
   1281 # row-wise access
   1282 # apply doesn't have a `na_action` keyword and for backward compat reasons
   1283 # we need to give `na_action="ignore"` for categorical data.
   1284 # TODO: remove the `na_action="ignore"` when that default has been changed in
   1285 #  Categorical (GH51645).
   1286 action = "ignore" if isinstance(obj.dtype, CategoricalDtype) else None
-> 1287 mapped = obj._map_values(
   1288     mapper=curried, na_action=action, convert=self.convert_dtype
   1289 )
   1291 if len(mapped) and isinstance(mapped[0], ABCSeries):
   1292     # GH#43986 Need to do list(mapped) in order to get treated as nested
   1293     #  See also GH#25959 regarding EA support
   1294     return obj._constructor_expanddim(list(mapped), index=obj.index)

File ~\AppData\Local\anaconda3\envs\myenv\lib\site-packages\pandas\core\base.py:921, in IndexOpsMixin._map_values(self, mapper, na_action, convert)
    918 if isinstance(arr, ExtensionArray):
    919     return arr.map(mapper, na_action=na_action)
--> 921 return algorithms.map_array(arr, mapper, na_action=na_action, convert=convert)

File ~\AppData\Local\anaconda3\envs\myenv\lib\site-packages\pandas\core\algorithms.py:1814, in map_array(arr, mapper, na_action, convert)
   1812 values = arr.astype(object, copy=False)
   1813 if na_action is None:
-> 1814     return lib.map_infer(values, mapper, convert=convert)
   1815 else:
   1816     return lib.map_infer_mask(
   1817         values, mapper, mask=isna(values).view(np.uint8), convert=convert
   1818     )

File lib.pyx:2917, in pandas._libs.lib.map_infer()

Cell In[59], line 5, in punc(words)
      4 def punc(words):
----> 5     no_punc = [char for char in words if char not in string.punctuation]
      6     join_punc = "".join(no_punc)
      8     return join_punc

TypeError: 'float' object is not iterable
0

There are 0 answers