0

I checked w_table.iloc[i,4] and didn't find NoneType objects in it. What's the matter could it be?

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)) 

After executing above code I am getting following 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
Dadep
  • 2,796
  • 5
  • 27
  • 40
losya
  • 3
  • 3
  • Welcome to StackOverflow. Please take the time to read this post on [how to provide a great pandas example](http://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) as well as how to provide a [minimal, complete, and verifiable example](http://stackoverflow.com/help/mcve) and revise your question accordingly. These tips on [how to ask a good question](http://stackoverflow.com/help/how-to-ask) may also be useful. – jezrael Jul 23 '18 at 11:31
  • But it seems problem is with `NaN`s, missing values in column. – jezrael Jul 23 '18 at 11:32
  • Can you provide an example of what `w_table` looks like and describe what your trying to do? Generally looping through a DataFrame isn't the best idea and there could be an easier solution here. – vielkind Jul 23 '18 at 11:32
  • @jezrael there are no 'Nan's in column – losya Jul 23 '18 at 11:34
  • How working `if pd.notnull(w_table.iloc[i, 4])` ? – jezrael Jul 23 '18 at 11:35
  • @vealkind there are some copies of the raws in table and I need to delete them – losya Jul 23 '18 at 11:35
  • @jezrael the command returns true – losya Jul 23 '18 at 11:36
  • check answer, problem is with append. – jezrael Jul 23 '18 at 11:37
  • 'check' is None. When using operator `in`, python will look for an iterable. So 'if (...) in check' yields this error if 'check is None'... – rafaelc Jul 23 '18 at 12:15

1 Answers1

0

The problem is with this line:

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

list.append is an in-place operation and returns None. Instead, just use:

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

For better performance, use set and set.add:

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

Even better, you can use vectorised functionality to avoid loops altogether. For this, you should provide a complete example, probably in a separate question.

jpp
  • 159,742
  • 34
  • 281
  • 339