TypeError:"float"类型的对象没有len()&TypeError:"float"对象不可迭代 [英] TypeError: object of type 'float' has no len() & TypeError: 'float' object is not iterable
问题描述
我有一个导入为DataFrame"new_data_words"的数据集.没有包含混乱网页名称的"page_name"列,例如"%D8%AA%D8%B5%D9%86%D9%8A%D9%81:%D8%A2%D9%84%D9"..
","%D9%85%D9%84%D9%81:IT-Airforce-OR2.png
"或简称为" 1950
".我想创建一个新列'word_count'以使页面名称中的单词数(单词由'_'分隔)
I have a dataset imported as DataFrame "new_data_words". There is a column "page_name" containing messy webpage names, like "%D8%AA%D8%B5%D9%86%D9%8A%D9%81:%D8%A2%D9%84%D9...
", "%D9%85%D9%84%D9%81:IT-Airforce-OR2.png
" or simply "1950
". I want to create a new column 'word_count' to have the count of words in the page name (words are delimited by '_')
这是我的代码:
拆分为单词:
b = list(new_data_words['page_name'].str.split('_'))
new_data_words['words'] = b
我检查了b的类型是 list 类型,len(b)是 6035980 .一个样本值:
I checked the type of b is list type and len(b) is 6035980. One sample value:
In [1]: new_data_words.loc[0,'words']
Out[2]: ['%D8%AA%D8%B5%D9%86%D9%8A%D9%81:%D8%A2%D9%84%D9%87%D8%A9',
'%D8%A8%D9%84%D8%A7%D8%AF',
'%D8%A7%D9%84%D8%B1%D8%A7%D9%81%D8%AF%D9%8A%D9%86']
我创建了另一列"word_count"以对"words"列的每一行中的列表元素进行计数.(必须使用循环触摸每一行中的列表元素)
I created another column "word_count" to count the elements of the list in each row of column "words". (Have to use loop to touch the elements of list in each row)
但是我有错误:
x = []
i = []
c = 0
for i in b: # i is list type, with elements are string, I checked
c=c+1
x.append(len(i))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-12-c0cf0cfbc458> in <module>()
6 #y = str(y)
7 c=c+1
----> 8 x.append(len(i))
TypeError: object of type 'float' has no len()
我不知道为什么它是浮点型的.....
I don't know why it is float type.....
但是,如果我仅添加打印件,它会起作用
However if I only add a print, it worked
x = []
i = []
c = 0
for i in b:
c=c+1
print len(i)
x.append(len(i))
3
2
3
2
3
1
8
...
但是c = len(x)= 68516,远小于600万.
But c = len(x) = 68516, much smaller than 6 millions.
我试图将元素再次强制为字符串,发生了另一个错误:
I tried to force the elements to be string again, another error happened:
x = []
for i in b:
for y in i:
y = str(y)
x.append(len(i))
TypeError Traceback (most recent call last)
<ipython-input-164-c86f5f48b80c> in <module>()
1 x = []
2 for i in b:
----> 3 for y in i:
4 y = str(y)
5 x.append(len(i))
TypeError: 'float' object is not iterable
我认为我是列表类型并且可以迭代...
I think i is list type and is iterable...
同样,如果我不附加,而仅打印,则可以:
Again, if I did not append, but only print, it worked:
x = []
for i in b:
for y in i:
y = str(y)
print (len(i))
另一个例子:这有效:
Another example: This works:
a = []
for i in range(10000):
a.append(len(new_data_words.loc[i,"words"]))
更改为动态范围,它不起作用:
Changed to a dynamic range, it does not work:
a = []
for i in range(len(b)):
a.append(len(new_data_words.loc[i,"words"]))
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-f9d0af3c448f> in <module>()
1 a = []
2 for i in range(len(b)):
----> 3 a.append(len(new_data_words.loc[i,"words"]))
TypeError: object of type 'float' has no len()
这也不起作用......
This does not work either......
a = []
for i in range(6035980):
a.append(len(new_data_words.loc[i,"words"]))
似乎列表中有一些异常.但是我不知道那是什么或如何找到它.
Seems like there are some abnormal in the list. But I don't know what that is or how to find it.
任何人都可以帮忙吗?
推荐答案
您错了.您所看到的错误使100%清楚地知道 b
是可迭代的,并且至少包含一个 float
(其他元素是 str
还是不,我不会推测).
You're wrong. The errors you're seeing make it 100% clear that b
is an iterable containing at least one float
(whether the other elements are str
or not I won't speculate).
尝试做:
for i in b:
print(type(i), i)
,您会看到至少有一个 float
.或仅打印 b
的不可迭代组件:
and you'll see there is at least one float
. Or this to only print the non-iterable components of b
:
import collections
for i in b:
if not isinstance(i, collections.Iterable):
print(type(i), i)
这篇关于TypeError:"float"类型的对象没有len()&TypeError:"float"对象不可迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!