0

I have a lambda function like this:

from Sastrawi.Stemmer.StemmerFactory import StemmerFactory

factory = StemmerFactory()
stemmer = factory.create_stemmer()

df['tweets_stemming'] = df['tweets_stopwords'].apply(lambda x: [stemmer.stem(stem) for stem in x])

And I want to convert this lambda function to function so I write this:

from Sastrawi.Stemmer.StemmerFactory import StemmerFactory

def stemmer_remover(stemming):
  factory = StemmerFactory()
  stemmer = factory.create_stemmer()
  stemming = [stemmer.stem(stemming) for stemming in df['tweets_stopwords']]

  return stemming

df['tweets_stemming'] = df['tweets_stopwords'].apply(stemmer_remover)

If using lambda function my code runs fine. But, if using function (not lambda) giving an error TypeError: descriptor 'lower' requires a 'str' object but received a 'list'. So, I think because my pandas dataframe from df['tweets_stopwords'] (see the image)

How to fix this?

tweets_stopwords column

2
  • 1
    In your own words, when you use .apply, how many times do you think the passed-in function gets called? What do you think gets passed to it? In your own words, in your version of the code using the function, how do you think the function is using the passed-in parameter? Commented Oct 1, 2021 at 23:11
  • 1
    A lambda expression, lambda <args>: <lambda body> can be simply replaced by def some_name(<args>): return <lambda body> Commented Oct 1, 2021 at 23:15

1 Answer 1

1

Your named function doesn't work the same way as your lambda.

lambda x: [stemmer.stem(stem) for stem in x]

is the same as:

def stemmer_remover(x):
    return [stemmer.stem(stem) for stem in x]

If you replace your lambda expression with the above stemmer_remover you should find that it behaves the same way.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.