mardi 17 avril 2018

Creating a new column and assigning it a value, based on an if condition

I have a dataframe: enter image description here

I want to iterate through it to check if it is a word in an NLTK supported reference. Within the if statement, I want to create a new column called 'bool' and assign a true or false depending on it the reference is good or not. I wrote the following code, and for some reason it returns all falses even though it ought to be a mix. Here is the code:

from nltk.corpus import wordnet

for each in df['word']:
    if not wordnet.synsets(each):
        df['new'] = False
        print('Not an English Word')       
    else:
        df['new'] = True
        print('English Word')  

df

The outputs are: enter image description here

and: enter image description here

The first output is fine. However, the second output should have a "True" in every other row in the column 'bool' like this: enter image description here

I dont' understand why my code is not able to accomplish this.

Thanks.

Aucun commentaire:

Enregistrer un commentaire