我正在尝试在 excel 中编写一个等于 isnumber[column] 函数的函数数据集:feature1 feature2 feature3 123 1.07 1 231 2.08 3 122 ab 4 111 3.04 6 555 cde 8feature1: integer dtypefeature2: object dtypefeature3: integer dtype我试过这段代码for item in df.feature2.iteritems(): if isinstance(item, float): print('yes') else: print('no')我得到的结果是 no no no no no但我想要结果yesyesnoyesno当我尝试检查单个 feature2 值的类型时,这就是所见type(df.feature2[0]) = strtype(df.feature2[1]) = strtype(df.feature2[2]) = strtype(df.feature2[3]) = strtype(df.feature2[4]) = strBut clearly 0,1,3 should be shown as float, but they show up as str我做错了什么?
3 回答
繁星淼淼
TA贡献1775条经验 获得超11个赞
Iteritems 返回一个元组,((123, '1.07'), 1.07)由于您想遍历每个值,请尝试以下代码。您只需要移除.iteritems()它,它就会像魅力一样发挥作用。
df['feature2']=[1.07,2.08,'ab',3.04,'cde']
for item in df.feature2:
if isinstance(item,float):
print('yes')
else:
print('no')
这是您的输出:
yes
yes
no
yes
no
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
试试这个:
for i in range(len(df["feature2"])):
test = df.loc[i,"feature2"]
if isinstance(test, float):
print('yes')
else:
print('no')
添加回答
举报
0/150
提交
取消