如果列清除,为什么会出现类型错误?

时间:2018-07-23 11:29:53

标签: python pandas ipython nonetype

我检查了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

1 个答案:

答案 0 :(得分:0)

问题出在这一行:

check = check.append(w_table.iloc[i, 4])

list.append就地操作,并返回None。相反,只需使用:

check.append(w_table.iloc[i, 4])

为获得更好的性能,请使用setset.add

check = {'word'}
...
check.add(w_table.iloc[i, 4])

更好的是,您可以使用向量化功能来完全避免循环。为此,您应该在separate question中提供一个完整的示例。