Home > OS >  TypeError: argument of type 'float' is not iterable. Trying to find rows which doesn'
TypeError: argument of type 'float' is not iterable. Trying to find rows which doesn'

Time:10-13

My dataframe looks like this(not exactly, but here is the sample):

    WARNSPEAK4    WARNSIGN_DTL
1    1.0      word bla bla bla...
2    nan    words do not contain...
3    nan    word bla bla..
4    1.0      word bla bla..
5    nan    no relation bla bla..

and I'm trying to find rows only have 'word' but without 'not'

here is my code:

# WARNSPEAK4 결측치를 WARNSIGN_DTL칼럼에서 찾을 수 있는지 확인해봅시다
df3 = df[['WARNSPEAK4','WARNSIGN_DTL']] # take columns only we need
df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word:',na=False) &
   df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x)]

and I'm getting this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [10], in <cell line: 3>()
      1 # WARNSPEAK4 결측치를 WARNSIGN_DTL칼럼에서 찾을 수 있는지 확인해봅시다
      2 df3 = df[['WARNSPEAK4','WARNSIGN_DTL']]
      3 df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word',na=False) &
----> 4    df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x )]

File ~\miniconda3\envs\py38\lib\site-packages\pandas\core\series.py:4433, in Series.apply(self, func, convert_dtype, args, **kwargs)
   4323 def apply(
   4324     self,
   4325     func: AggFuncType,
   (...)
   4328     **kwargs,
   4329 ) -> DataFrame | Series:
   4330     """
   4331     Invoke function on values of Series.
   4332 
   (...)
   4431     dtype: float64
   4432     """
-> 4433     return SeriesApply(self, func, convert_dtype, args, kwargs).apply()

File ~\miniconda3\envs\py38\lib\site-packages\pandas\core\apply.py:1088, in SeriesApply.apply(self)
   1084 if isinstance(self.f, str):
   1085     # if we are a string, try to dispatch
   1086     return self.apply_str()
-> 1088 return self.apply_standard()

File ~\miniconda3\envs\py38\lib\site-packages\pandas\core\apply.py:1143, in SeriesApply.apply_standard(self)
   1137         values = obj.astype(object)._values
   1138         # error: Argument 2 to "map_infer" has incompatible type
   1139         # "Union[Callable[..., Any], str, List[Union[Callable[..., Any], str]],
   1140         # Dict[Hashable, Union[Union[Callable[..., Any], str],
   1141         # List[Union[Callable[..., Any], str]]]]]"; expected
   1142         # "Callable[[Any], Any]"
-> 1143         mapped = lib.map_infer(
   1144             values,
   1145             f,  # type: ignore[arg-type]
   1146             convert=self.convert_dtype,
   1147         )
   1149 if len(mapped) and isinstance(mapped[0], ABCSeries):
   1150     # GH#43986 Need to do list(mapped) in order to get treated as nested
   1151     #  See also GH#25959 regarding EA support
   1152     return obj._constructor_expanddim(list(mapped), index=obj.index)

File ~\miniconda3\envs\py38\lib\site-packages\pandas\_libs\lib.pyx:2870, in pandas._libs.lib.map_infer()

Input In [10], in <lambda>(x)
      1 # WARNSPEAK4 결측치를 WARNSIGN_DTL칼럼에서 찾을 수 있는지 확인해봅시다
      2 df3 = df[['WARNSPEAK4','WARNSIGN_DTL']]
      3 df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word',na=False) &
----> 4    df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x )]

TypeError: argument of type 'float' is not iterable

I don't understand why I'm getting this error cause df3['WARNSIGN_DTL'] column is string type, not float

CodePudding user response:

You may try to use parentheses when you use '&' conditions, from :

df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word',na=False) &
df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x )]

to this

df3[(df3['WARNSPEAK4'].isnull()) & (df3['WARNSIGN_DTL'].str.contains('word',na=False)) & (df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x ))]
  • Related