【问题标题】:Why does the type error appear if the column is clear?列清了为什么会出现类型错误?
【发布时间】:2018-07-23 11:29:53
【问题描述】:

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

【问题讨论】:

  • 欢迎来到 StackOverflow。请花时间阅读how to provide a great pandas example 上的这篇文章以及如何提供minimal, complete, and verifiable example 并相应地修改您的问题。 how to ask a good question 上的这些提示也可能有用。
  • 但似乎问题出在NaNs,列中缺少值。
  • 你能提供一个w_table 的例子并描述你想要做什么吗?通常循环遍历 DataFrame 并不是最好的主意,这里可能有更简单的解决方案。
  • @jezrael 列中没有“Nan”
  • if pd.notnull(w_table.iloc[i, 4]) 工作怎么样?

标签: python pandas ipython nonetype


【解决方案1】:

问题出在这一行:

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 中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-03-29
    • 2016-01-04
    • 1970-01-01
    • 1970-01-01
    • 2021-03-05
    • 1970-01-01
    相关资源
    最近更新 更多