TypeError: object of type 'float' has no len() & TypeError: 'float' object is not iterable
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' 以计算页面名称中的字数(字由“_”分隔)
这是我的代码:
拆分为单词:
b = list(new_data_words['page_name'].str.split('_'))
new_data_words['words'] = b
我检查了b的类型是list类型,len(b)是6035980。
一个样本值:
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" 每一行中的列表元素。 (必须使用循环来触摸每一行中的列表元素)
但我有错误:
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()
不知道为什么是float类型.....
但是,如果我只添加一个印刷品,它就起作用了
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万
我试图再次将元素强制为字符串,发生另一个错误:
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 是列表类型并且是可迭代的...
同样,如果我没有追加,而只是打印,它就起作用了:
x = []
for i in b:
for y in i:
y = str(y)
print (len(i))
另一个例子:
这有效:
a = []
for i in range(10000):
a.append(len(new_data_words.loc[i,"words"]))
改成动态范围,不行:
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()
这个也不行……
a = []
for i in range(6035980):
a.append(len(new_data_words.loc[i,"words"]))
列表中似乎有些异常。但我不知道那是什么或如何找到它。
有人可以帮忙吗?
你错了。您看到的错误使 b
是一个至少包含一个 float
的可迭代对象(我不会推测其他元素是否为 str
)。b
是一个可迭代对象。 =17=]
尝试做:
for i in b:
print(type(i), i)
你会看到至少有一个 float
。或者只打印 b
:
的不可迭代组件
import collections
for i in b:
if not isinstance(i, collections.Iterable):
print(type(i), i)
我将一个数据集导入为 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' 以计算页面名称中的字数(字由“_”分隔)
这是我的代码:
拆分为单词:
b = list(new_data_words['page_name'].str.split('_'))
new_data_words['words'] = b
我检查了b的类型是list类型,len(b)是6035980。 一个样本值:
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" 每一行中的列表元素。 (必须使用循环来触摸每一行中的列表元素)
但我有错误:
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()
不知道为什么是float类型.....
但是,如果我只添加一个印刷品,它就起作用了
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万
我试图再次将元素强制为字符串,发生另一个错误:
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 是列表类型并且是可迭代的...
同样,如果我没有追加,而只是打印,它就起作用了:
x = []
for i in b:
for y in i:
y = str(y)
print (len(i))
另一个例子: 这有效:
a = []
for i in range(10000):
a.append(len(new_data_words.loc[i,"words"]))
改成动态范围,不行:
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()
这个也不行……
a = []
for i in range(6035980):
a.append(len(new_data_words.loc[i,"words"]))
列表中似乎有些异常。但我不知道那是什么或如何找到它。
有人可以帮忙吗?
你错了。您看到的错误使 b
是一个至少包含一个 float
的可迭代对象(我不会推测其他元素是否为 str
)。b
是一个可迭代对象。 =17=]
尝试做:
for i in b:
print(type(i), i)
你会看到至少有一个 float
。或者只打印 b
:
import collections
for i in b:
if not isinstance(i, collections.Iterable):
print(type(i), i)