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