更新时间:2023-09-26 13:16:46
如果数据缺少DIAGNOS
的值,则可能发生TypeError: 'float' object is not iterable
.例如,当数据如下所示:
The TypeError: 'float' object is not iterable
could happen if the data is missing a value for DIAGNOS
. For example, when data looks like this:
LopNr AR var3 va4 var5 var6 var7 var8 var9 var10 DIAGNOS
6 2011 a a a a a a a a S834
6 2011 a a a a a a a a
6 2011 a a a a a a a a K21 S834
然后
In [68]: treatments = pd.read_table('data', usecols=[0,1,10])
In [69]: treatments
Out[69]:
LopNr AR DIAGNOS
0 6 2011 S834
1 6 2011 NaN
2 6 2011 K21 S834
[3 rows x 3 columns]
DIAGNOS
列中的NaN
是问题的根源,因为str.split(' ')
保留NaN:
The NaN
in the DIAGNOS
column is the source of the problem, since str.split(' ')
preserves the NaN:
In [70]: diagnoses = treatments['DIAGNOS'].str.split(' ')
In [71]: diagnoses
Out[72]:
0 [S834]
1 NaN
2 [K21, S834]
Name: DIAGNOS, dtype: object
当调用diganose.map(tobacco)
时,NaN
被传递给tobacco
函数.由于NaN
是浮点且不可迭代,因此for x in lst
循环会引发TypeError
.
The NaN
gets passed to the tobacco
function when diganose.map(tobacco)
is called. Since NaN
is a float and not iterable, the for x in lst
loop raises the TypeError
.
为避免此错误,请替换treatments['DIAGNOS']
中的NaN:
To avoid this error, replace the NaNs in treatments['DIAGNOS']
:
import pandas as pd
def tobacco(lst):
return any((('C30' <= x < 'C40') or ('F17' <= x <'F18')) for x in lst)
treatments = pd.read_table('data', usecols=[0,1,10])
treatments['DIAGNOS'].fillna('', inplace=True)
diagnoses = treatments['DIAGNOS'].str.split(' ')
treatments['tobacco'] = diagnoses.map(tobacco)
print(treatments)
收益
LopNr AR DIAGNOS tobacco
0 6 2011 S834 False
1 6 2011 False
2 6 2011 K21 S834 False
[3 rows x 4 columns]