列明了为什么会出现类型错误?
Why does the type error appear if the column is clear?
我检查了 w_table.iloc[i,4]
并没有在其中找到 NoneType
个对象。会不会是什么问题?
check = ['word']
for i in range(len(w_table)):
if w_table.iloc[i, 4] != 'Null':
if w_table.iloc[i, 4] in check:
w_table = w_table.drop(w_table.index[i])
else:
check = check.append(w_table.iloc[i, 4])
w_table.index = np.arange(len(w_table))
执行完上面的代码后,我开始关注 TypeError
TypeError Traceback (most recent call
last) <ipython-input-74-40b9156195fa> in <module>()
2 for i in range(len(w_table)):
3 if w_table.iloc[i, 4] != 'Null':
4 if w_table.iloc[i, 4] in check:
5 w_table = w_table.drop(w_table.index[i])
6 else:
TypeError: argument of type 'NoneType' is not iterable
这一行有问题:
check = check.append(w_table.iloc[i, 4])
list.append
是 就地操作 和 returns None
。相反,只需使用:
check.append(w_table.iloc[i, 4])
为了获得更好的性能,请使用 set
和 set.add
:
check = {'word'}
...
check.add(w_table.iloc[i, 4])
更好的是,您可以使用矢量化功能来完全避免循环。为此,您应该提供一个完整的示例,可能在 separate question.
中
我检查了 w_table.iloc[i,4]
并没有在其中找到 NoneType
个对象。会不会是什么问题?
check = ['word']
for i in range(len(w_table)):
if w_table.iloc[i, 4] != 'Null':
if w_table.iloc[i, 4] in check:
w_table = w_table.drop(w_table.index[i])
else:
check = check.append(w_table.iloc[i, 4])
w_table.index = np.arange(len(w_table))
执行完上面的代码后,我开始关注 TypeError
TypeError Traceback (most recent call
last) <ipython-input-74-40b9156195fa> in <module>()
2 for i in range(len(w_table)):
3 if w_table.iloc[i, 4] != 'Null':
4 if w_table.iloc[i, 4] in check:
5 w_table = w_table.drop(w_table.index[i])
6 else:
TypeError: argument of type 'NoneType' is not iterable
这一行有问题:
check = check.append(w_table.iloc[i, 4])
list.append
是 就地操作 和 returns None
。相反,只需使用:
check.append(w_table.iloc[i, 4])
为了获得更好的性能,请使用 set
和 set.add
:
check = {'word'}
...
check.add(w_table.iloc[i, 4])
更好的是,您可以使用矢量化功能来完全避免循环。为此,您应该提供一个完整的示例,可能在 separate question.
中