mardi 17 avril 2018

list index out of range error in for-if loop

I have a dataframe called 'df' with one column called 'word' that looks like this:

enter image description here

I have code that checks if i in df.word is a word, if it is then 'True' is added to the list 'boo'. While the code works, for some reason it keeps throwing out the error message: Error: The Following Error occured: list index out of range, after a true result is processed:

df=pd.read_csv(r"C:\Users\hiner\for_pydict.csv")
boo = list()
for i in range df.word:
meaning=dictionary.meaning(i)
if meaning is not None:
    d = 'True'
else:
    d = 'False'
boo.append(d)  

The immediate out put is:

Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range
Error: The Following Error occured: list index out of range

But 'boo' has the result I want:

boo
['True', 'False', 'True', 'False', 'True']

First, I don't understand what part of the code the index error is referring to. Second, how can I prevent the error message from printing/displaying. Do I need to change my code, or do I need to apply exception handling and if so, how would I do that?

Thanks.

Aucun commentaire:

Enregistrer un commentaire