Toxing words into a new column in a pandas frame

I am trying to look at the list of comments compiled in the pandas framework, and tag these words and put these words in a new column in the data framework, but I have an error going through this,

The error is that the AttributeError: 'unicode' object does not have the 'apwords' attribute

Is there any other way to do this? Thanks

def apwords(words): filtered_sentence = [] words = word_tokenize(words) for w in words: filtered_sentence.append(w) return filtered_sentence addwords = lambda x: x.apwords() df['words'] = df['complaint'].apply(addwords) print df 
0
source share
2 answers

Your way to apply the lambda function is right, this is how you define addwords that doesn't work.

When you define apwords , you define function not a attribute , so when you want to apply it, use:

 addwords = lambda x: apwords(x) 

And not:

 addwords = lambda x: x.apwords() 

If you want to use apwords as an attribute, you will need to define a class that inherits from string and define apwords as an attribute in this class.

It is much easier to stay with function :

 def apwords(words): filtered_sentence = [] words = word_tokenize(words) for w in words: filtered_sentence.append(w) return filtered_sentence addwords = lambda x: apwords(x) df['words'] = df['complaint'].apply(addwords) 
0
source

Don't you want to do this:

  df['words'] = df['complaint'].apply(apwords) 

you do not need to define the addwords function. Which should be defined as:

 addwords = lambda x: apwords(x) 
0
source

Source: https://habr.com/ru/post/1268209/


All Articles