My dataframe looks like this(not exactly, but here is the sample):
JavaScript
x
7
1
WARNSPEAK4 WARNSIGN_DTL
2
1 1.0 word bla bla bla
3
2 nan words do not contain
4
3 nan word bla bla..
5
4 1.0 word bla bla..
6
5 nan no relation bla bla..
7
and I’m trying to find rows only have ‘word’ but without ‘not’
here is my code:
JavaScript
1
5
1
# WARNSPEAK4 결측치를 WARNSIGN_DTL칼럼에서 찾을 수 있는지 확인해봅시다
2
df3 = df[['WARNSPEAK4','WARNSIGN_DTL']] # take columns only we need
3
df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word:',na=False) &
4
df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x)]
5
and I’m getting this error:
JavaScript
1
56
56
1
---------------------------------------------------------------------------
2
TypeError Traceback (most recent call last)
3
Input In [10], in <cell line: 3>()
4
1 # WARNSPEAK4 결측치를 WARNSIGN_DTL칼럼에서 찾을 수 있는지 확인해봅시다
5
2 df3 = df[['WARNSPEAK4','WARNSIGN_DTL']]
6
3 df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word',na=False) &
7
----> 4 df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x )]
8
9
File ~miniconda3envspy38libsite-packagespandascoreseries.py:4433, in Series.apply(self, func, convert_dtype, args, **kwargs)
10
4323 def apply(
11
4324 self,
12
4325 func: AggFuncType,
13
( )
14
4328 **kwargs,
15
4329 ) -> DataFrame | Series:
16
4330 """
17
4331 Invoke function on values of Series.
18
4332
19
( )
20
4431 dtype: float64
21
4432 """
22
-> 4433 return SeriesApply(self, func, convert_dtype, args, kwargs).apply()
23
24
File ~miniconda3envspy38libsite-packagespandascoreapply.py:1088, in SeriesApply.apply(self)
25
1084 if isinstance(self.f, str):
26
1085 # if we are a string, try to dispatch
27
1086 return self.apply_str()
28
-> 1088 return self.apply_standard()
29
30
File ~miniconda3envspy38libsite-packagespandascoreapply.py:1143, in SeriesApply.apply_standard(self)
31
1137 values = obj.astype(object)._values
32
1138 # error: Argument 2 to "map_infer" has incompatible type
33
1139 # "Union[Callable[..., Any], str, List[Union[Callable[..., Any], str]],
34
1140 # Dict[Hashable, Union[Union[Callable[..., Any], str],
35
1141 # List[Union[Callable[..., Any], str]]]]]"; expected
36
1142 # "Callable[[Any], Any]"
37
-> 1143 mapped = lib.map_infer(
38
1144 values,
39
1145 f, # type: ignore[arg-type]
40
1146 convert=self.convert_dtype,
41
1147 )
42
1149 if len(mapped) and isinstance(mapped[0], ABCSeries):
43
1150 # GH#43986 Need to do list(mapped) in order to get treated as nested
44
1151 # See also GH#25959 regarding EA support
45
1152 return obj._constructor_expanddim(list(mapped), index=obj.index)
46
47
File ~miniconda3envspy38libsite-packagespandas_libslib.pyx:2870, in pandas._libs.lib.map_infer()
48
49
Input In [10], in <lambda>(x)
50
1 # WARNSPEAK4 결측치를 WARNSIGN_DTL칼럼에서 찾을 수 있는지 확인해봅시다
51
2 df3 = df[['WARNSPEAK4','WARNSIGN_DTL']]
52
3 df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word',na=False) &
53
----> 4 df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x )]
54
55
TypeError: argument of type 'float' is not iterable
56
I don’t understand why I’m getting this error cause df3[‘WARNSIGN_DTL’] column is string type, not float
Advertisement
Answer
Use parentheses when you use ‘&’ conditions. Change it from :
JavaScript
1
3
1
df3[df3['WARNSPEAK4'].isnull() & df3['WARNSIGN_DTL'].str.contains('word',na=False) &
2
df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x )]
3
to this
JavaScript
1
2
1
df3[(df3['WARNSPEAK4'].isnull()) & (df3['WARNSIGN_DTL'].str.contains('word',na=False)) & (df3['WARNSIGN_DTL'].apply(lambda x: 'not' not in x ))]
2